destructi6n destructi6n -4 years ago 144
C# Question

How to post multiple models in one post

I have the following scenario of adding a product, I want the user to enter some SKU information for that product as well. The product and sku have a one to one relationship and I want to post two models/objects in one create action post. How would one achieve this?

The action signature would look like this

public ActionResult Create(Product product, Sku sku)

what would my razor markup need to look like in order to achieve this? Would I need two forms? Or is there something built into MVC that I haven't seen yet?

Answer Source

You need another model which contains all your required stuff like this:

public Class SomeClassName
 public Product product{get;set;}
 public Sku sku{get;set;}

and then you need to send it instead

public ActionResult Create(SomeClassName data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download