Why isn't MVC able to bind my List of complex objects? I have read several examples on this topic and nothing has resolved the issue. Consider the following:
Complex Version
I have a ViewModel with a list of complex objects as a property:
public List<XMLVariable> XMLVariables { get; set; }
My View then binds this list of objects to controls:
@for (int i = 0; i < Model.XMLVariables.Count(); i++)
{
@Html.HiddenFor(m => Model.XMLVariables[i].Name)
@Html.TextBoxFor(m => Model.XMLVariables[i].Value)
}
The view displays the variables just fine, but when I submit the form nothing happens. The controller action is not called.
Flattened Version
If I flatten my list of complex objects (XMLVariables) in the same ViewModel:
public List<string> Names { get; set; }
public List<string> Values { get; set; }
And map them in my View:
@for (int i = 0; i < Model.XMLVariables.Count(); i++)
{
@Html.HiddenFor(m => Model.Names[i])
@Html.TextBoxFor(m => Model.Values[i])
}
Upon submission both lists are passed back with the correct values.
Controller Action:
[HttpPost]
public ActionResult Submit(ReportViewModel report, string cmd)
{
}
Form Signature:
@using (Ajax.BeginForm("Submit", "Report", new AjaxOptions {LoadingElementId = "progress", OnSuccess="OnSuccess", OnBegin="OnBegin", OnComplete="OnComplete" }))