我有一个基本的viewmodel,其属性是复杂类型的列表。当绑定,我似乎坚持得到值的列表,或其他模型属性取决于发布的值(即视图安排)。
视图模型:
public class Myviewmodel { public int Id { get; set; } public string Property1 { get; set; } public string Property2 { get; set; } public List<MyDataItem> Data { get; set; } } public class MyDataItem { public int Id { get; set; } public int ParentId { get; set; } public string Name { get; set; } public string Value { get; set; } }
控制器操作:
public ActionResult MyForm() { Myviewmodel model = new Myviewmodel(); model.Id = 1; model.Data = new List<MyDataItem>() { new MyDataItem{ Id = 1,ParentId = 1,Name = "MyListItem1",Value = "SomeValue"} }; return View(model); } [HttpPost] public ActionResult MyForm(Myviewmodel model) { //... return View(model); }
这里是基本视图(没有列表标记)
@using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>My View Model</legend> @Html.HiddenFor(model => model.Id) <div class="editor-label"> @Html.LabelFor(model => model.Property1) </div> <div class="editor-field"> @Html.EditorFor(model => model.Property1) @Html.ValidationMessageFor(model => model.Property1) </div> <div class="editor-label"> @Html.LabelFor(model => model.Property2) </div> <div class="editor-field"> @Html.EditorFor(model => model.Property2) @Html.ValidationMessageFor(model => model.Property2) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> }
当发布回控制器时,我得到2个属性值和一个null值为’数据’属性如预期。
如果我添加列表的标记如下(基于Scott Hanselman post和Phil Haack’s post中的信息):
<div class="editor-field"> @for (int i = 0; i < Model.Data.Count(); i++) { MyDataItem data = Model.Data[i]; @Html.Hidden("model.Data[" + i + "].Id",data.Id) @Html.Hidden("model.Data[" + i + "].ParentId",data.ParentId) @Html.Hidden("model.Data[" + i + "].Name",data.Name) @Html.TextBox("model.Data[" + i + "].Value",data.Value) } </div>
发布的表单值如下:
Id=1&Property1=test1&Property2=test2&model.Data%5B0%5D.Id=1&model.Data%5B0%5D.ParentId=1&model.Data%5B0%5D.Name=MyListItem1&model.Data%5B0%5D.Value=SomeValue
有没有办法获得两组属性填充或我只是错过一些明显的东西?
编辑:
对于那些谁是好奇的。基于MartinHN的答案,原始生成的标记是:
<div class="editor-field"> <input id="model_Data_0__Id" name="model.Data[0].Id" type="hidden" value="1" /> <input id="model_Data_0__ParentId" name="model.Data[0].ParentId" type="hidden" value="1" /> <input id="model_Data_0__Name" name="model.Data[0].Name" type="hidden" value="MyListItem1" /> <input id="model_Data_0__Value" name="model.Data[0].Value" type="text" value="SomeValue" /> </div>
<div class="editor-field"> <input id="Data_0__Id" data-val="true" name="Data[0].Id" type="hidden" value="1" data-val-number="The field Id must be a number." data-val-required="The Id field is required." /> <input id="Data_0__ParentId" name="Data[0].ParentId" type="hidden" value="1" data-val="true" data-val-number="The field ParentId must be a number." data-val-required="The ParentId field is required." /> <input id="Data_0__Name" name="Data[0].Name" type="hidden" value="MyListItem1" /> <input id="Data_0__Value" name="Data[0].Value" type="text" value="SomeValue" /> </div>
其结果是以下发布的值:
Id=1&Property1=test1&Property2=test2&Data%5B0%5D.Id=1&Data%5B0%5D.ParentId=1&Data%5B0%5D.Name=MyListItem1&Data%5B0%5D.Value=SomeValue
注意没有’模型’。在名称和发布的值…