我无法将DropDownListFor绑定到我的模型。 DisplayOrder返回null,VarietyFamilyID始终为0.我有一个未知数量的对象要编辑,所以我尝试使用EditorTemplate。
EditorTemplate视图
@model Wysocki_Variety_Management.Models.Image
<div>
@Html.HiddenFor(i=>i.ImageStreamID)
@Model.helperString
@Html.DropDownListFor(i=>i.DisplayOrder, new SelectList(ViewBag.DisplayOptions, "Key", "Value"))
@Html.DropDownListFor(i => i.VarietyFamilyID, new SelectList(ViewBag.Variety, "VarietyFamilyID", "Name"))
@Ajax.ActionLink("Try Me",
"UpdateImage",
"Images",
new { s = Model.ImageStreamID, dislplayOrder = Model.DisplayOrder, familyID = Model.VarietyFamilyID },
new AjaxOptions {})
</div>
查看渲染模板
@model List<Wysocki_Variety_Management.Models.Image>
@using (Html.BeginForm("UpdateImage", "Images"))
{
<div>
@Html.EditorForModel()
</div>
}
控制器操作签名
public ActionResult UpdateImage(Guid s, int displayOrder,int familyID)
修改
不确定投票或评论。当我使用EditorTemplate和@ Html.EditorForModel时,我为模型的每个成员获得了多个输入。
<form action="/Images/UpdateImage" method="post"> <div>
<div>
<input data-val="true" data-val-required="The Guid field is required." name="[0].ImageStreamID" type="hidden" value="e48aab34-0fe6-e511-8ebf-005056c00008" />
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[0].DisplayOrder"><option value="312">Tuber</option>
<option value="313">Plant</option>
<option value="314">Marketing</option>
</select>
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[0].VarietyFamilyID"><option value="0">Fianna</option>
<option value="0">Linzer Delikatesse</option>
<option value="3">Markies</option>
<option value="0">Provento</option>
<option value="5">Roko</option>
<option value="6">75-11-15 Sprint</option>
<option value="7">Alegria</option>
</select>
<a data-ajax="true" href="/Images/UpdateImage?s=e48aab34-0fe6-e511-8ebf-005056c00008&dislplayOrder=0&familyID=0">Try Me</a>
</div><div>
<input data-val="true" data-val-required="The Guid field is required." name="[1].ImageStreamID" type="hidden" value="e58aab34-0fe6-e511-8ebf-005056c00008" />
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[1].DisplayOrder"><option value="312">Tuber</option>
<option value="313">Plant</option>
<option value="314">Marketing</option>
</select>
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[1].VarietyFamilyID"><option value="0">Fianna</option>
<option value="0">Linzer Delikatesse</option>
<option value="3">Markies</option>
<option value="0">Provento</option>
<option value="5">Roko</option>
<option value="6">75-11-15 Sprint</option>
<option value="7">Alegria</option>
</select>
<a data-ajax="true" href="/Images/UpdateImage?s=e58aab34-0fe6-e511-8ebf-005056c00008&dislplayOrder=0&familyID=0">Try Me</a>
</div><div>
<input data-val="true" data-val-required="The Guid field is required." name="[2].ImageStreamID" type="hidden" value="e68aab34-0fe6-e511-8ebf-005056c00008" />
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[2].DisplayOrder"><option value="312">Tuber</option>
<option value="313">Plant</option>
<option value="314">Marketing</option>
</select>
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[2].VarietyFamilyID"><option value="0">Fianna</option>
<option value="0">Linzer Delikatesse</option>
<option value="3">Markies</option>
<option value="0">Provento</option>
<option value="5">Roko</option>
<option value="6">75-11-15 Sprint</option>
<option value="7">Alegria</option><option value="331">A02507-2LB</option>
</select>
<a data-ajax="true" href="/Images/UpdateImage?s=e68aab34-0fe6-e511-8ebf-005056c00008&dislplayOrder=0&familyID=0">Try Me</a>
</div><div>
<input data-val="true" data-val-required="The Guid field is required." name="[3].ImageStreamID" type="hidden" value="e78aab34-0fe6-e511-8ebf-005056c00008" />
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[3].DisplayOrder"><option value="312">Tuber</option>
<option value="313">Plant</option>
<option value="314">Marketing</option>
</select>
<select data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." name="[3].VarietyFamilyID"><option value="0">Fianna</option>
<option value="0">Linzer Delikatesse</option>
<option value="3">Markies</option>
<option value="0">Provento</option>
<option value="5">Roko</option>
<option value="6">75-11-15 Sprint</option>
<option value="7">Alegria</option>
答案 0 :(得分:0)
您视图中的模型为IEnumerable<Image>
,因此要在提交表单时进行绑定,方法必须
[HttpPost]
public ActionResult UpdateImage(IEnumerable<Image> model)
和model将包含3个正确绑定的Image
个对象。
表单根据成功表单控件的name
和value
属性发回名称/值对,DefaultModelBinder
与您的参数和模型属性匹配。目前,您的方法中的参数为Guid s, int displayOrder, int familyID
,并且值可以绑定到这些参数的唯一方法是,您的表单包含name="s"
,name="displayOrder"
和name="familyID"
的控件。 / p>
请注意,在@Ajax.ActionLink()
中使用EditorTemplate
只会回发模型的初始值。如果要发布表单控件的值,则需要提交表单。