Lets say I have a partial view that contains both a checkbox and a numerical value.
I have a ViewModel that contains a Model — Terms — that implements the partial view. When I submit it, the modifications made in the Terms Partial View does not reflect to the Terms property of ViewModel. I’m probably misunderstanding a concept or another on how it works, anyone care to point it out please?
View
@model ViewModel
@using (Html.BeginForm("ViewAction", "ViewController", FormMethod.Post))
{
// Other ViewModel Property Editors
@Html.Partial("Terms", Model.Terms)
<input type="submit" value="Submit" />
}
Partial View
@model Terms
@Html.CheckBoxFor(m => m.IsAccepted)
@Html.EditorFor(m => m.NumericalValue)
Controller
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult ViewAction(int id)
{
ViewModel vm = GetVmValues();
return View(vm);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ViewAction(ViewModel vm)
{
// Access ViewModel properties
}
The default model binder expects the control ids for your Terms model to be named
Terms.IsAcceptedandTerms.NumericalValue. You need to create an editor template for yourTermsmodel and then call@Html.EditorFor(m=> m.Terms)instead of using a partial.You can read more about editor templates here. Its from MVC 2, but should still be relevant.