这是 MVC3 form in partial view 的后续内容
尽管作者给出了解决该问题的方法。我想找到一个正确的答案,为什么这不起作用。
我无法访问原始代码,但这是我的代码(因此我可以回答以下问题):
// main view (which is partial too)
@foreach (AddingComponentVM sc in Model)
{
@Html.Partial("_SearchIngredientUpdate", sc);
}
//partial view
@using (Ajax.BeginForm("IngredientSearchUpdate", new { controller = "Recipe" }, ajxOpt, new { id = "addingWidgetForm" + Model.IngredientID }))
{
@Html.TextBoxFor(model => model.IngredientID)
@Model.IngredientID
}
@Model.IngredientID 包含正确的值。但是文本框包含发送到 Controller 的模型值(原文如此!),并且对于每个表单来说显然都是相同的。
[AjaxOnly]
public JsonResult IngredientSearchUpdate(
AddingComponentVM dataIn,
[ModelBinder(typeof(SearchOptionsBinder))] SearchOptions sessionSO)
如果签名中没有 AddComponentVM 的操作调用上面相同的代码,表单将正确呈现。
public PartialViewResult IngredientSearch([ModelBinder(typeof(SearchOptionsBinder))] SearchOptions sessionSO)
任何人都可以指出我这种奇怪行为(至少对我来说)的原因吗?谢谢!
最佳答案
我因此无法休眠,但答案是:
使用 PartialView 还是 EditorTemplates 并不重要。如此处所述:How to modify posted form data within controller action before sending to view?
“HTML Helpers 在尝试查找键时使用以下顺序优先级:
- ViewData.ModelState 字典条目
- 模型属性(如果强 键入的 View 。此属性是 View.ViewData.Model 的快捷方式)
- ViewData 字典条目”
因此,如果发布了任何值,则足以清除 StateModel 集合,然后可以通过使用的 html 帮助程序获取模型中的数据。这就能解决问题:
ModelState.Clear()
关于asp.net-mvc-3 - 部分 View 中的表单 - 需要正确的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11798169/