Submitting a collection of items in ASP.NET MVC

I have two classes in my MVC project Orderand Product.

Since Productyou can order many times, and Ordercan have many products, I have a third entity, which OrderedProduct. He combines these two objects in many ways.

Now what I'm trying to do is give the user the opportunity to place an order by putting products from the drop-down list in the box, and then save my order. In addition, the client must fill in some files in the Order entity, such as address data, etc. All I want is all on one page. The user can add as many items from the drop-down list as he wants, then he can add the entire order.

To simplify the visualization, look at this image:

Layout http://img245.imageshack.us/img245/4618/myimage.png

Now the problem is how to implement this behavior in my MVC application. Should I create a ViewModel that combines a class Orderand a list Productor uses a partial view for classes Product?

Just like my main problem , how can I (in an elegant way) get a complete list of selected products or at least the product identifier in the controller after a POST request? In this case, how can I indicate that what I'm sending is a set of identifiers? Just add one object, but what about an entire collection?

This is a place where I do not fully understand asp.net MVC, so please give me some bright ideas;) I welcome you all, thanks for the consultation for all your answers!

+3
source share
1 answer

The desired interface seems a bit confusing to me, but here, how you can handle these things ... change your desire. I assume that OrderedProductthis is an order object containing the product identifier and quantity, and that you want to be able to change them all at the same time.

Order ViewModel. , List<OrderedProduct>, OrderedProducts:
ViewModel OrderedProduct. , / / , . : views/shared/EditorTemplates OrderedProduct.ascx.

Order , NewOrderedProduct OrderedProduct. Order : <%=Html.EditorFor(m=>m.OrderedProducts)%> .
: <%= Html.EditorFor(m=> m.NewOrderedProduct) %> .
[POST] Order.

, , NewOrderedProduct , OrderedProducts . , , .

. [POST] .

HTML. Ajaxify, .

Edit
, OrderedProduct ,

+2

Source: https://habr.com/ru/post/1758536/


All Articles