I have the following ViewModel
public class NewCustomerViewModel {
public long CustomerId {get;set;}
public NewCustomerFormViewModel NewCustomerForm {get;set; }
}
NewCustomerFormViewModel contains only fields which are part of the form. And only them I would like to validate. So I would like to have the following Action signature:
[HttpPost]
public ActionResult NewCustomer(long battleId,
NewCustomerFormViewModel newCustomerFormViewModel)
Unfortunately it doesn’t work with default ModelBinder. So what is the easiest way to bind properties of NewCustomerViewModel to NewCustomerFormViewModel?
Update. Here is the part of my view:
Title @Html.TextBoxFor(m => m.NewCustomerForm.Title,
Model.NewCustomerForm.Title)
So ModelBinder looks for NewCustomerForm property which he can’t find in NewCustomerForViewModel
ASP.NET MVC relies heavily upon naming conventions for resolving Views, Actions, Controllers, and even parameters. The parameters are probably being posted under a
NewCustomerFormnamespace (e.g.NewCustomerForm.Name=John), because that’s how they fit into the model that you’re producing inputs for. Try renaming your parameter tonewCustomerFormso that the binder actually does find an object by that name.