Suppose I have a model like this:
public class Foo {
public List<Bar> Bars { get; set; }
public string Comment { get; set; }
}
public class Bar {
public int Baz { get; set; }
}
And I need a view Foothat allows users to edit elements Bar. But with a trick: I want editing to Barbe handled by a partial view.
@model Web.ViewModels.Foo
@using(Html.BeginForm()) {
@Html.Partial("_EditBars", Model.Bars)
@Html.TextAreaFor(m => m.Comment)
...
}
A partial view _EditBarslooks something like this:
@model List<Web.ViewModels.Bar>
@for (int i = 0; i < Model.Count; i++) {
@Html.EditorFor(m => Model[i].Baz)
}
I want this to simulate a binding to my action, which looks something like this:
[HttpPost]
public ActionResult Edit(Foo foo) {
}
Unfortunately, this is the data I publish that does not model property binding Bars:
[1].Baz=10&[0].Baz=5&Comment=bla
It makes sense that this does not work because the prefix is missing Bars. If I understand correctly, I need this to be:
Bars[1].Baz=10&Bars[0].Baz=5&Comment=bla
So, I tried this approach :
@Html.Partial(
"_EditBars",
Model.Bars,
new ViewDataDictionary(ViewData)
{
TemplateInfo = new TemplateInfo
{
HtmlFieldPrefix = "Bars"
}
})
But that doesn't work either. In doing so, I get:
Bars.[1].Baz=10&Bars.[0].Baz=5&Comment=bla
, - (Bars.[1] vs. Bars[1]). -, , , ?
: . , - , , , , EditorTemplate Bar EditorFor . , .