1

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'.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Possible duplicate http://stackoverflow.com/questions/14038392/editorfor-ienumerablet-with-templatename – Zabavsky Jan 31 '13 at 20:28
  • @Zabavsky Wonderful!, believe me, I searched through SO for over an hour and I never hit that question. Thanks! – Icarus Jan 31 '13 at 20:31

1 Answers1

3

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'

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • How about with using `@Html.EditorForModel()`? Do you get the same issue? – Russ Cam Jan 31 '13 at 20:26
  • I must be doing something stupid then because when I do `@Html.EditorFor(x=>x)` 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'.` – Icarus Jan 31 '13 at 20:27
  • Yep, same exception as before: `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'` – Icarus Jan 31 '13 at 20:29
  • 1
    My bad. I've just run up a demo to test it and you're right. Looks like in inconsistency in the framework as if you change the EditorTemplate name to `Person` and simple use `@Html.EditorForModel()` you'll get the expected result. So it looks like it doens't work with non default named template names. Will update answer. – Russ Cam Jan 31 '13 at 20:34
  • Thanks for your answer. It was very helpful. I had no idea that the template name had to match the Model name exactly. – Icarus Jan 31 '13 at 20:44