Reputation: 32448
I have a model that contains a collection. I want to wrap each individual EditorFor
in a form that posts to an action with a single element as the parameter. i.e:
class ModelWithCollection
{
List<ElementModel> Elements { get; set; }
}
class ElementModel
{
int Field { get; set; }
}
public ActionResult ElementAction(ElementModel viewModel)
{
}
However, my viewModel
doesn't have the properties populated. I can understand this, as the editor renders the ids as things like Elements[0].Field
. Rather where the action would like just Field
.
How can I get around this?
I don't think I can manually build the post using jquery or something as it include uploading a file.
Upvotes: 2
Views: 46
Reputation: 5666
You can workaround this using the partial view. If you move your EditorFor
to partial view (with ElementModel
as model) there won't be generated [0]
brackets.
Upvotes: 1
Reputation: 42497
There is an overload of EditorFor
that takes three arguments: expression
, templateName
, and htmlFieldName
. If you supply the expression, null
for templateName
(thereby telling the helper to resolve the editor template the usual way), and an empty string for htmlFieldName
, this should render the fields without the Elements[0].
prefix.
<%=Html.EditorFor(m => m.Elements[i], null, string.Empty)%>
Upvotes: 2