Possible Duplicate:
View Model IEnumerable<> property is coming back null (not binding) from post method?
I am having a problem that is pretty nearly identical to the problem specified here:
View Model IEnumerable<> property is coming back null (not binding) from post method?
My problem differs from the question linked above, in that in my example I am procedurally creating data with which to populate my view model.
However, none of the proposed solutions fix the issue for me. The method I show below is identical, as far as I can tell, to Travis J's answer, however I also tried the method involving creating an EditorTemplate for my model and/or "sub-model". I'm new to MVC, so I'm most likely doing something staggeringly, obviously wrong.
Here is a streamlined example that shows my problem:
Models:
public class Foo
{
public string Bar { get; set; }
public Foo(string bar)
{
Bar = bar;
}
public Foo(){}
}
public class Foos
{
public List<Foo> ListOfFoos {get;set;}
}
Controller:
public ActionResult Index()
{
Foos foos = new Foos
{
ListOfFoos = new List<Foo>()
};
// here I'm procedurally constructing some data to keep my example simple.
for (int i = 0; i < 10; ++i)
{
foos.ListOfFoos.Add(new Foo(i.ToString()));
}
return View( foos );
}
[HttpPost]
public ActionResult Index(Foos foos)
{
??? foos.ListOfFoos is null here ???
return View();
}
View:
@model OCC_Tracker.Models.Foos
@using (Html.BeginForm())
{
<table>
<tbody>
@for (int i = 0; i < Model.ListOfFoos.Count; ++i)
{
<tr>
<td>
@Html.TextBoxFor(m => m.ListOfFoos[i].Bar)
</td>
</tr>
}
</tbody>
</table>
<button>Submit</button>
}