Mvc3 form for IEnumerable

I have a model for which I would like to have a bulk update page, but I have problems.

My ViewModel:

public class ApproveView { public IEnumerable<MyObject> ObjectList { get; set; } } 

In my opinion, 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 traverses my objects and prints a shape. The problem is that all fields of the same type have the same name. So, for example, all my switches are connected, and I can choose only one.

How to make the names for each field unique and associated with this object? Am I even on the right track or is there a better way to do this?

+7
source share
2 answers

Check this post: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

You need to create an editor for your entity.

Hope this helps.

+4
source

You can also do this somewhat manually:

  @{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++; @} 
0
source

All Articles