Aaron Reese
Aaron Reese

Reputation: 131

how to make httppost actionresult bind to only part of the viewModel

I have the following sudo-code

class viewModel
{
    public ICollection<modelA> parentModel
    public modelC formModel
}

class modelA
{
    public int ID {get;set;}
    public virtual Icollection<modelB> {get;set;}
}

class modelB
{
    public int ID {get;set;}
    public string SomeString {get;set;}
    public virtual modelA ModelA {get;set;}
}

class modelC
{
    public int ModelAID {get;set;}
    public string SomeString {get;set;}
}

So. the view model contains a collection of As. Each A contains a collection of Bs and there is a separate model for posting back as a form: the form will be repeated on the page, once in each instance of A with the A.ID passed in to ModelAID as a hidden field. Only one form posting is allowed on the page, The id of the form fields are formModel.ModelAID and .formModel.SomeString as they are derived from the non-parent element of the viewModel.

How do I get the ActionResult to bind only to formModel?

 [HttpPost]
 Public ActionResult Input(formModel vm)
 {
    ... by default the view model being passed back is full VM, I only want the formModel so the post signature does not match
 }

Upvotes: 0

Views: 174

Answers (1)

Ali Baig
Ali Baig

Reputation: 3867

You can try something like

public ActionResult Input([Bind(Prefix = "formModel ")]modelC model)
{

}

Upvotes: 1

Related Questions