在 ASP.NET MVC 中发送项目集合

发布于 2024-09-13 20:21:56 字数 813 浏览 6 评论 0原文

我的 MVC 项目中有两个类 OrderProduct

因为 Product 可以多次订购,并且 Order 可以有许多产品,所以我得到了第三个实体,即 OrderedProduct。它将这两个实体以多对多关系连接起来。

现在我想做的是让用户通过将下拉列表中的产品放入框中来下订单,然后保存我的订单。此外,客户还必须在订单实体中填写一些字段,例如地址数据等。我想要的只是将其全部放在一页上。用户可以根据需要从下拉列表中添加任意数量的项目,然后他可以添加整个订单。

为了更容易可视化,请查看这张图片:

布局

现在的问题是如何在我的 MVC 应用程序中实现此类行为。我应该构建一个结合了 Order 类和 Product 列表的 ViewModel 还是使用 Product 类的部分视图?

这也是我的主要问题,我如何(以优雅的方式)在 POST 请求后检索所选产品的完整列表或至少在控制器中检索产品 ID?在这种情况下,我如何指定我发送的是 id 集合?添加一个对象很简单,但是添加整个集合呢?

这是我不完全理解asp.net MVC的地方,所以请给我一些好主意;)向大家问好,感谢您的所有回答的建议!

I've got two classes in my MVC project Order and Product.

Because Product can be ordered many times and Order can have many products I've got third entity which is OrderedProduct. It joins those two entities in many-to-many relation.

Now what I'm trying to do is to let user to make an order by putting products from the drop down list to the box and then to save my order. Also client have to fill some fileds in the Order entity such as Address data etc. All I want is to have it all on one single page. User can add as many items from dropdown as he like, then he can add whole order.

To make it easier to visualize look at this picture:

Layout

Now the problem is how to implement such behaviour in my MVC app. Should I build a ViewModel that combines Order class and list of Product or use partial view for Product classes?

Also which is my main problem, how can I (in elegant way) retrieve the full list of chosen products or at least product id's in the controller after POST request? In this case how can I specify that what I'm sending is a collection of ids? It's simple to add one object, but what about whole collection?

This is the place when I do not fully understand asp.net MVC, so please give me some bright ideas ;) Greetings to you all, thanks in advice for all your answers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短暂陪伴 2024-09-20 20:21:56

所需的界面对我来说似乎有点令人困惑,但这是您处理这些事情的方法......根据您的愿望进行修改。我假设 OrderedProduct 是一个用于订购的对象,其中包含产品 ID 和数量,并且您希望能够同时修改它们。

让您的 ViewModel Order 。假设它有一个名为 OrderedProductsList 属性:
使用 OrderedProduct 的 ViewModel 创建一个小型编辑器控件。没有表单,只有一个文本框/下拉菜单/绑定到产品名称属性的任何内容以及绑定到产品数量的文本框。 重要:将此控件放在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。

编辑
已更新以反映 OrderedProduct 对象的功能并更正帖子接受的类型

The desired interface seems a bit confusing to me but here's how you can handle these things.. modify to your desire. I'm going to assume OrderedProduct is an object for ordering that contains a Product ID and a quantity and that you want to be able to modify them all at the same time.

Make Order your ViewModel. Given that it has a List<OrderedProduct> property called OrderedProducts:
Create a small editor control with a ViewModel of OrderedProduct. No form, just a textbox/dropdown/whatever bound to the product name property and a textbox bound to the product quantity. Important: put this control in views/shared/EditorTemplates and call it OrderedProduct.ascx.

Give your Order object a property called NewOrderedProduct of type OrderedProduct.
In the form for the Order view do: <%=Html.EditorFor(m=>m.OrderedProducts)%> this will give you an editable list current items in the order.
After that do: <%= Html.EditorFor(m=> m.NewOrderedProduct) %> this is where you can add new items.
Have the [POST] action take a type of Order.

Now when it submits if the NewOrderedProduct property is valid you can add it to the OrderedProducts list and redisplay. You can add as many as you want this way and they will all autobind.

Have a separate submit button in the form for submitting the order. Check for the existence of that button's name in the [POST] and submit all at one time.

This gets you a straight HTML working version. From there you can Ajaxify it if you want.

Edit
Updated to reflect the function of the OrderedProduct object and correct the type the post accepts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文