I have a view like so:
@model Foo
@Html.DisplayFor(model => model.FooName);
@{
int counter = -1;
}
@foreach(var bar in Model.Bars)
{
counter++;
<tr>
<td><span name = "Bars[@counter].BarName">@bar.BarName</span></td>
</tr>
}
where:
class Foo
{
public string FooName { get; set; }
public ICollection<Bar> Bars { get; set; }
}
class Bar
{
public string BarName { get; set; }
}
Even though my view has it right, when I post back the Foo
, though, it doesn't post back its Bars
.
public ActionResult DoStuffWithFoo(Foo foo)
{
Debug.Assert(foo.Bars != null); // fails
}
I have omitted the unnecessary and obvious parts from this question such as the mark up of a table.
You should understand that on form
post you post only values in inputs
so you should to something like this:
@for(var i = 0; i < Model.Bars.Count(); i++)
{
<tr>
<td><span>@Html.TextBoxFor(x=> Model.Bars[i].BarName)</span></td>
</tr>
}
Html.TextBoxFor
helper create input
that you can post to controller.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments