Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 400
lovefaithswing Avatar asked Sep 21 '11 20:09

lovefaithswing


1 Answers

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.

like image 154
Samich Avatar answered Oct 03 '22 11:10

Samich