I have a form which needs to populate 2 models. Normally I use a ModelBinderAttribute on the forms post action i.e.
[Authorize]
[AcceptVerbs("POST")]
public ActionResult Add([GigBinderAttribute]Gig gig, FormCollection formCollection)
{
///Do stuff
}
In my form, the fields are named the same as the models properties...
However in this case I have 2 different models that need populating.
How do I do this? Any ideas? Is it possible?
In MVC we cannot pass multiple models from a controller to the single view.
You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.
Yes, you can use Tuple (brings magic in view having multiple model).
Actually... the best way is to do this:
public ActionResult Add([GigBinderAttribute]Gig gig, [FileModelBinderAttribute]File file) {
}
You CAN use multiple attributes!
In cases like this, I tend to make a single model type to wrap up the various models involved:
class AddModel
{
public Gig GigModel {get; set;}
public OtherType OtherModel {get; set;}
}
...and bind that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With