Using the EditorFor templates is a really nice feature of ASP.Net MVC 3, but is it possible to get EditorFor to render an unpopulated template to allow for creation of records?
Or is there some other way to do this?
The ways in which I am trying to do this is as follows:
@Html.EditorFor(model => model)
@Html.EditorFor(x => new List<Business.ViewModel.Affiliate.Contact>())
@Html.EditorFor(new List<Business.ViewModel.Affiliate.Contact>())
@Html.EditorFor(new Business.ViewModel.Affiliate.Contact())
The first one obviously works, however the subsequent ones (which demonstrate what I am trying to do) all fail with the following error:
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
The model in question is:
IEnumerable<Business.ViewModel.Affiliate.Contact>
It’s the responsibility of the controller to prepare the view model that will be passed to the view. So if you need for example to initialize your view view model with 5 empty contact rows you could do this simply in your controller:
and in your view use the EditorFor helper as usual:
This will render the corresponding editor template for each of the 5 elements we have added to the
Contactscollection.