mvc3 form for a IEnumerable
I have a model that I would like have a mass update page for, but am having trouble.
My ViewModel:
public class ApproveView
{
public IEnumerable<MyObject> ObjectList { get; set; }
}
In my view I have:
foreach (var item in Model.ObjectList)
{
<div>
<table class="form" width="100%">
<tr>
<td>@Html.LabelFor(model => item.Accurate)<br />
@Html.RadioButtonFor(model => item.Accurate, true) Yes
@Html.RadioButtonFor(model => item.Accurate, false) No
@Html.ValidationMessageFor(model => item.Accurate)
</td>
<td>
@Html.LabelFor(model => item.Comments)<br />
@Html.TextAreaFor(model => item.Comments)<br />
@Html.ValidationMessageFor(model => item.Comments)
</td>
</tr>
</table>
@Html.HiddenFor(model => item.ID)
@Html.HiddenFor(model => item.CreatedOn)
@Html.HiddenFor(model => item.CreatedBy)
@Html.HiddenFor(model => item.ModifiedOn)
@Html.HiddenFor(model => item.ModifiedBy)
<hr />
}
This loops over my objects and prints the form. The trouble is that all of the fields of the same type have the same name. So, for example, all of my radio buttons are connected and I can only select one.
how do I make the names for each field unique and associated with that object? Am I even on the right track or is there a better way to do this?
Check this post: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
You need to create editor for your entity.
Hope it helps.
你也可以手动做一些:
@{var count = 0;}
@foreach (var item in Model.ObjectList){
<div>
<table class="form">
<tr>
<td>@Html.Label("Accurate" + count, item.Accurate)<br />
@Html.RadioButton("AccurateTrue" + count, true) Yes
@Html.RadioButton("AccurateFalse" + count, false) No
@Html.ValidationMessage("ValidationAccurate" + count, item.Accurate)
</td>
<td>
@Html.Label("CommentsLabel" + count, item.Comments)<br />
@Html.TextArea("Comments" + count, item.Comments)<br />
@Html.ValidationMessage("ValidationComment" + count, item.Comments)
</td>
</tr>
</table>
@Html.Hidden("ID" + count, item.ID)
@Html.Hidden("CreatedOn" + count, item.CreatedOn)
@Html.Hidden("CreatedBy" + count, item.CreatedBy)
@Html.Hidden("ModifiedOn" + count, item.ModifiedOn)
@Html.Hidden("ModifiedBy" + count, item.ModifiedBy)
<hr />
@count++;
@}
链接地址: http://www.djcxy.com/p/54840.html
上一篇: 计划UILocalNotifications iOS设备备份的一部分?
下一篇: IEnumerable的mvc3表单