ASP。NET MVC部分视图与表单

时间:2022-11-30 17:33:43

I have a scenario I want to use a partial view but I'm having issues with it passing data to the controller. Here is a basic example of what I'm trying to do.

我有一个场景,我想使用局部视图,但是我在它向控制器传递数据时遇到了问题。这是我要做的一个基本的例子。

Objects:

对象:

  • Customer
  • 客户
  • Order
  • 订单

A Customer has an IList<Order> on it. I want the partial view to allow the user to edit the information. I can get the data to display but when the form posts the list under the Customer object is null.

客户有一个IList 。我希望部分视图允许用户编辑信息。我可以显示数据,但当表单发布Customer对象下的列表为空时。

I have also attempted to use a seperate form in my partial view. When I do this if I create paramenters on the controller like so I get the data:

我也尝试在我的部分观点中使用一个分开的形式。当我这样做的时候,如果我在控制器上创建参数,就像我得到数据:

public ActionResult UpdateOrders(IList<Guid> id, IList<int> quantity, IList<Guid> productId)

But when I do this

但是当我这么做的时候

public ActionResult UpdateOrders(IList<Order> orders)

The list is null.

列表为空。

If anyone has a better suggestion of how to achieve this let me know.

如果有人有更好的建议如何实现这一点,请告诉我。

1 个解决方案

#1


2  

How are you referencing the fields in your view? I'm thinking that it should be something like:

如何引用视图中的字段?我想应该是这样的:

<input type="hidden" name="orders.Index" value="0" />
<input type="hidden" name="oders[0].ID" value="1" />
<input type="hidden" name="orders[0].productId" value="4" />
<input type="text" name="orders[0].quantity" value="6" />

<input type="hidden" name="orders.Index" value="1" />
<input type="hidden" name="orders[1].ID" value="2" />
<input type="hidden" name="orders[1].productId" value="2" />
<input type="text" name="orders[1].quantity" value="15" />

See Phil Haack's blog entry on binding to a list for more info.

有关更多信息,请参阅Phil Haack关于绑定到列表的博客条目。

#1


2  

How are you referencing the fields in your view? I'm thinking that it should be something like:

如何引用视图中的字段?我想应该是这样的:

<input type="hidden" name="orders.Index" value="0" />
<input type="hidden" name="oders[0].ID" value="1" />
<input type="hidden" name="orders[0].productId" value="4" />
<input type="text" name="orders[0].quantity" value="6" />

<input type="hidden" name="orders.Index" value="1" />
<input type="hidden" name="orders[1].ID" value="2" />
<input type="hidden" name="orders[1].productId" value="2" />
<input type="text" name="orders[1].quantity" value="15" />

See Phil Haack's blog entry on binding to a list for more info.

有关更多信息,请参阅Phil Haack关于绑定到列表的博客条目。