views:

113

answers:

1

In this question I am looking for links/code to handle an IList<OrderLine> in an MVC 2 edit form. Specifically I am interested in sending a complete order to the client, then posting the edited order back to an object (to persist) using:

Html.EditorFor(m => m.orderlines[i]) (where orderlines is an enumerable object)

Editing an Order that has multiple order lines (two tables, Order and OrderLine, one to many) is apparently difficult. Is there any links/examples/patterns out there to advise how to create this form that edits an entity and related entities in a single form (in C# MVC 2)?

The IList is really throwing me for a loop. Should I have it there (while still having one form to edit one order)? How could you use the server side factory to create a blank OrderLine in the form while not posting the entire form back to the server? I am hoping we don't treat the individual order lines with individual save buttons, deletes, etc. (for example, they may open an order, delete all the lines, then click cancel, which shouldn't have altered the order itself in either the repository nor the database.

Example classes:

public class ViewModel {
    public Order            order       {get;set;}  // Only one order
}

public class Order {
    public int              ID          {get;set;} // Order Identity
    public string           name        {get;set;}
    public IList<OrderLine> orderlines  {get;set;} // Order has multiple lines
}

public class OrderLine {
    public int              orderID     {get;set;} // references Order ID above
    public int              orderLineID {get;set;} // Order Line identity (need?)
    public Product          refProduct  {get;set;} // Product value object
    public int              quantity    {get;set;} // How many we want
    public double           price       {get;set;} // Current sale price
}
+3  A: 

You need to understand List<>/Array/IEnumerable model binding:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

jfar
Wow, that Stevens Anderson one is incredible. That's exactly what I needed.
Dr. Zim