views:

364

answers:

1

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.

+2  A: 

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.

tvanfosson
The fields are generated by a foreach so they end up with the same id and name currently.
cjibo
Your suggestion lead me to finding a solution. I had to add the .index line I forgot it. Not sure whey we don't have a helper for that yet.
cjibo