Search code examples
asp.net-mvcmodel-binders

ASP.NET MVC - Multiple models in a form and model binders


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?


Solution

  • Actually... the best way is to do this:

    public ActionResult Add([GigBinderAttribute]Gig gig, [FileModelBinderAttribute]File file) {
    

    }

    You CAN use multiple attributes!