I've got a viewmodel that contains other viewmodels.
public class AggregateVM
{
public BrandVM BrandVM { get; set; }
public TinTypeVM TinTypeVM { get; set; }
}
When I http post to the controller action, the TinTypeVM is populated with my edited values, but the BrandVM viewmodel where I used a partial is always null.
Here's are the view.
@model SaveEF.ViewModels.AggregateVM
@using (Html.BeginForm("EditAggregate", "Aggregate"))
{
@Html.Partial("_EditBrand", Model.BrandVM)
@Html.Label("Tin Name")
@Html.EditorFor(model => model.TinTypeVM.Name)
<input type="submit" value="Save" />
}
Here's the partial view.
@model SaveEF.ViewModels.BrandVM
@Html.Label("Brand Name")
@Html.EditorFor(model => model.Name)
Here's the controller action.
public ActionResult EditAggregate(AggregateVM vm)
{
SaveBrand(vm.BrandVM);
SaveTinType(vm.TinTypeVM);
return RedirectToAction("Index");
}
How can I use partials in the view and still pass a single view model into the EditAggregate action? I've tried different parameters in Html.BeginForm("EditAggregate", "Aggregate", FormMethod.Post, new { vm = Model })) but that didn't help.
name
attributes of your partial are not prefixed correctly. One option is to pass theHtmlFieldPrefix
as per this answer – user3559349EditorTemplate
- rename you partial toBrandVM.cshtml
and put it in the/Views/Shared/EditorTemplates
folder - then in the view its just@Html.EditorFor(m => m.BrandVM)
(the solution you accepted might work but makes no sense - it now means you cannot use that partial in other models, so you are defeating the whole purpose of using a partial) – user3559349