zmb*_*mbq 6 c# binding asp.net-mvc-3
我有一个ASP.NET MVC应用程序,显示项目列表.在我的视图页面中,我遍历项目并使用局部视图渲染每个项目,如下所示:
@foreach(var item in Model.items)
{
<li>
@Html.Partial("ItemView", item)
</li>
}
Run Code Online (Sandbox Code Playgroud)
在项目视图中,我使用具有"删除"按钮的表单来包装每个项目,如下所示:
@using(Html.BeginForm(...))
{
@Html.HiddenFor(m=>m.Id)
<label>@Model.Name (@Model.Id)</label>
<input type="submit" value="Delete"/>
}
Run Code Online (Sandbox Code Playgroud)
这些项目正确呈现,生成的页面包含所有项目的清单,其中显示了正确的名称和ID.
编辑:@Hidden也是如此,显然,与我之前写的相反.
此外,这仅在第二次呈现表单时发生(即,在单击其中一个"删除"按钮后),第一次一切正常.我的动作方法如下所示:
public ActionResult AllItems()
{
var model = new AllItemsModel();
return PartialView(model);
}
public ActionResult Delete(DeleteModel model)
{
.... Perform the delete ...
return PartialView("AllItems", new AllItemsModel());
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我怀疑这是因为你Id
的RouteData中已经有一个参数:
public ActionResult SomeAction(int id)
{
var model = ...
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
并且您已经请求了该页面/somecontroller/someaction/123
.HiddenFor帮助程序现在使用路由值中的Id而不是项目的ID.尝试将项目视图模型上的属性重命名为不同于id的内容.例如ItemId
.
另一种可能性是问题仅在回发之后发生,而不是在最初呈现页面时发生.显示您的POST动作可能有助于进一步探索这种可能性.
更新:
好了,既然你已经展示了你的POST动作,事情就更清楚了:
public ActionResult Delete(DeleteModel model)
{
.... Perform the delete ...
return PartialView("AllItems", new AllItemsModel());
}
Run Code Online (Sandbox Code Playgroud)
您基本上是在这里创建一个新的视图模型并将其传递给局部视图.但HTML帮助程序在绑定时始终使用ModelState中的值.并且只有在您的视图模型中的值之后.因此,如果您打算在POST操作中修改模型的属性,请确保首先从ModelState中删除了此值.在您的示例中,因为您完全划破了整个视图模型(通过创建一个new AllItemsModel()
),您可以清除整个ModelState:
public ActionResult Delete(DeleteModel model)
{
.... Perform the delete ...
// Clear the modelstate otherwise the view will use the values that were initially posted
// and not the values from your view model
ModelState.Clear();
return PartialView("AllItems", new AllItemsModel());
}
Run Code Online (Sandbox Code Playgroud)
此行为是设计使然,适用于所有HTML帮助程序,而不仅适用于HiddenFor帮助程序.
归档时间: |
|
查看次数: |
866 次 |
最近记录: |