Hello,
Recently I updated MVC3 project to MVC4 and I found a strange behavor of default model binder. In some cases he can't bind form parameters to collections of my viewmodel. I built a project for demonstration this problem.
My viewmodel classes:
public class DefaultViewModel
{
public int Id { get; set; }
public List<PickerItem> Rights { get; set; }
public string RightsComment { get; set; }
}
public class PickerItem
{
public string Name { get; set; }
public string Value { get; set; }
}
In the view:
@using (Html.BeginForm("Index", "Home"))
{
<input type="text" name="Id" value="1" />
<input type="text" name="Rights[0].Name" value="Some name 1" />
<input type="text" name="Rights[0].Value" value="Some value 1" />
<input type="text" name="RightsComment" value="Some comment" />
<input type="submit" value="Submit" />
}
Controller action:
[HttpPost]
public ActionResult Index(DefaultViewModel model)
{
if (model.Rights == null)
throw new OperationCanceledException("Property Rights was not binded");
return RedirectToAction("Index");
}
Model binder skip Rights property collection in this case. Moreover, if I delete 'id' input from the form the binding will be correct...
I attach the project that demonstrates this problem
Comments: Verified.
Recently I updated MVC3 project to MVC4 and I found a strange behavor of default model binder. In some cases he can't bind form parameters to collections of my viewmodel. I built a project for demonstration this problem.
My viewmodel classes:
public class DefaultViewModel
{
public int Id { get; set; }
public List<PickerItem> Rights { get; set; }
public string RightsComment { get; set; }
}
public class PickerItem
{
public string Name { get; set; }
public string Value { get; set; }
}
In the view:
@using (Html.BeginForm("Index", "Home"))
{
<input type="text" name="Id" value="1" />
<input type="text" name="Rights[0].Name" value="Some name 1" />
<input type="text" name="Rights[0].Value" value="Some value 1" />
<input type="text" name="RightsComment" value="Some comment" />
<input type="submit" value="Submit" />
}
Controller action:
[HttpPost]
public ActionResult Index(DefaultViewModel model)
{
if (model.Rights == null)
throw new OperationCanceledException("Property Rights was not binded");
return RedirectToAction("Index");
}
Model binder skip Rights property collection in this case. Moreover, if I delete 'id' input from the form the binding will be correct...
I attach the project that demonstrates this problem
Comments: Verified.