ASP.NET MVC 2: Mechanics behind an Order / Order Line in a edit form
- by Dr. Zim
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
}