ASP.NET MVC Partial View with Form

左心房为你撑大大i 提交于 2019-12-08 01:20: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.

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:


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.



来源:https://stackoverflow.com/questions/1033635/asp-net-mvc-partial-view-with-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!