Search code examples
asp.net-mvcasp.net-mvc-4razor-2

How to create an EditorFor a collection and avoid doing a loop


For example:

Model

public class Person
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string  LastName { get; set; }
}

Editor Template for Person (PersonEditor.cshtml):

@model MvcApplication1.Models.Person

@Html.HiddenFor(x=>x.ID)
<label>First Name</label>    
@Html.TextBoxFor(x=>x.FirstName)
<label>Last Name</label>    
@Html.TextBoxFor(x=>x.LastName)
<br />

On my main page, I want to be able to do the following:

@model IList<MvcApplication1.Models.Person>

@using (Html.BeginForm())
{       
    @Html.EditorFor(x=>x,"PersonEditor")    
}

And have all the elements in the form, generate the proper names automatically; instead of having to loop through the collection as I am doing now:

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        @Html.EditorFor(x=>Model[i],"PersonEditor")    
    }   
}

The form elements must contain the following format:

<input name="[0].ID" type="text" value="Some ID" />
<input name="[0].FirstName" type="text" value="Some value" />
<input name="[1].ID" type="text" value="Some x" />
<input name="[1].FirstName" type="text" value="Some y" />

And so on...

Because in my controller, I expect to receive an IList<Person> when the form posts pack.

Can I eliminate that for loop completely?

EDIT

Right now, when I simply do @Html.EditorFor(x=>x) (without the loop, in other words), I get this exception:

The model item passed into the dictionary is of type 'MvcApplication1.Models.Person[]', but this dictionary requires a model item of type 'MvcApplication1.Models.Person'.


Solution

  • You should be able to use the same template for both IEnumerable<T> and T. The Templating is smart enough to enumerate the IEnumerable, but you will need to rename the editor template to match the type name. Then you should be able to use

    @model IList<MvcApplication1.Models.Person>
    
    @using (Html.BeginForm())
    {       
        @Html.EditorForModel()    
    }
    

    Unfortunately, it looks like a template named anything other than the type name will throw an exception

    The model item passed into the dictionary is of type 'MvcApplication1.Models.Person[]', but this dictionary requires a model item of type 'MvcApplication1.Models.Person'