我有一个奇怪的问题,我有一个编辑视图,它为段塞呈现禁用的文本框,所以用户可以看到他正在编辑的内容的弹格,但不能更改它。只有在启用textbox的Create视图上才能更改弹格。在编辑视图中,只能更改标题。
在编辑视图中,弹格文本框是从传入的ViewModel中填充的,这是可行的。
但是,当回发更改时,代码段塞不会在编辑视图中发布一种内容类型(我相信,这是正确的行为),因此Model.IsValid (因为我将所需的属性附加到段塞属性)会失败。如前所述,这很可能是正确的行为。
这件事是在编辑视图中的另一种类型的内容,禁用的文本框的内容被张贴和张贴视图模型是有效的,我甚至可以看到的值,段塞不变。
我可以发布代码,但这是相当大的视图,但是显示标题和段塞文本框的代码的相关部分对于Create/Edit视图和跨两种内容类型都是相同的,如下所示:
<div class="editor-label">
<%= Html.LabelFor(model => model.Title, Resources.Localize.Section_Title)%>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Title) %>
<%= Html.ValidationMessageFor(model => model.Title) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Slug, Resources.Localize.Section_Slug)%>
</div>
<div class="editor-field">
<% if (string.IsNullOrWhiteSpace(Model.Slug))
{ %>
<%= Html.TextBoxFor(model => model.Slug)%>
<% }
else
{ %>
<%= Html.TextBoxFor(model => model.Slug, new { disabled = true })%>
<% } %>
<%= Html.ValidationMessageFor(model => model.Slug) %>
</div>我使用一个部分视图来创建和编辑,这就是为什么有if...else.语句,因为我们只在编辑时禁用弹格文本框。
希望有人能知道这件事,因为我就是搞不懂。
更新:
最可怕的事。这些线
<form id="Page-CreateEditForm" action="<%=Url.Action(Url.RequestContext.RouteData.GetRequiredString("action"), Url.RequestContext.RouteData.GetRequiredString("controller")) %>" enctype="multipart/form-data" method="post">
<form id="Section-CreateEditForm" action="<%=Url.Action(Url.RequestContext.RouteData.GetRequiredString("action"), Url.RequestContext.RouteData.GetRequiredString("controller")) %>" enctype="multipart/form-data" method="post">在我看来(这两种观点都是一样的)产生了以下内容:
编辑页面“测试页”时的表单开始标记。测试页。注意行动中的子弹!
<form id="Page-CreateEditForm" action="/Page/Edit/test-page" enctype="multipart/form-data" method="post">编辑“测试部分”时的表单开始标记。子弹:测试部分。注意编辑后的子弹是如何丢失的。
<form id="Section-CreateEditForm" action="/Section/Edit" enctype="multipart/form-data" method="post">现在的问题是,为什么相同的表单标记定义会在运行时导致两个不同的操作?
后续行动:
当然,经过快速思考,这必须来自于global.asax.cs中的路由定义,所以让我们来看看它。这就是我们发现的:
routes.MapRoute(Localize.Routes_PagesEdit, RouteType.Admin, "Page/Edit/{slug}",
new {controller = "Page", action = "Edit", slug = ""}, null);
routes.MapRoute(
Localize.Routes_SectionsEdit,
RouteType.Regular,
"Section/Edit",
new {controller = "Section", action = "Edit"}, null
);查看区段编辑路由是如何缺少{slug}参数的。我相信我已经接近解决方案了。
发布于 2010-07-24 09:18:08
实际上,这是路径问题(缺少段塞参数)。显然,默认的模型绑定器能够将URL查询字符串参数绑定到POST上的ViewModel,因此即使禁用了弹格的文本框,也可以从查询字符串中获得值。
改变了这个
routes.MapRoute(
Localize.Routes_SectionsEdit,
RouteType.Regular,
"Section/Edit",
new {controller = "Section", action = "Edit"}, null
); 到这个
routes.MapRoute(
Localize.Routes_SectionsEdit,
RouteType.Regular,
"Section/Edit/{slug}",
new {controller = "Section", action = "Edit"}, null
); 解决了这个问题。
https://stackoverflow.com/questions/3314212
复制相似问题