2

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
  • Can you post the HTML, I think it would help in this question Commented Jun 23, 2009 at 16:45

1 Answer 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.

Sign up to request clarification or add additional context in comments.

3 Comments

The fields are generated by a foreach so they end up with the same id and name currently.
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: you may want to consider this post for your creation of the index... stackoverflow.com/questions/5013578/… Namely, look at the link from Steve Sanderson's Blog that gives you a codebase to generate the names with Guids, which works quite well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.