0

Does anyone know of any mvc samples that implement a one to many editing situation in a single or multiple views?

For example I'm looking for something that would show how to best implement a simple relationship of Order to OrderItem where OrderItem contains a Product reference and a Quantity. Sample models would be as follows:

public class Order     
{
   public int OrderID { get;set;}
   public string Name { get;set;}

   public ICollection<OrderItem> OrderItems { get;set;}
}

public class Product
{
   public int ProductID { get;set;}
   public string Name { get;set;}
}

public class OrderItem
{
   public int OrderItemID { get;set;}
   public Product Product { get;set;}
   public int Quantity { get;set;}
   public Order Order { get;set;}
}

I've looked at a lot of the samples out there and they are for simple CRUD operations without any navigation properties etc. Also, if possible I'd love to figure out how to edit an existing Order, adding, removing, changing OrderItem's without persisting to the database until the Save action on the Order is run.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Richard Edwards
  • 1,379
  • 1
  • 17
  • 30
  • What are you using for a db layer? Ado.net, Entity Framework, Linq to Sql, NHibernate, LlblGen, MongoDb, LightSpeed, Subsonic, hand-rolled? – John Farrell Dec 09 '10 at 19:42
  • I'm using Entity Framework. I'm not really having much trouble with the persistence, I'm just looking for a general example on how to structure the controllers, actions, views etc. I have a lot of the simple scenarios working fine such as CRUD operations on simple lookup items ie. Product but it's the one-to-many editing operations I'm struggling with. For instance, say I'm editing an Order using the Edit action of the OrderController and applicable view named EditOrder. How do I go about setting up the controller to add an OrderItem to the OrderItems collection and edit that. – Richard Edwards Dec 09 '10 at 20:58

2 Answers2

0

I don't quite get your question. What is the problem you are trying to solve?

If you are looking for general ideas for working with MCV+EF, then look for repository pattern.

If I understand the editing without persisting to the database until Save action part, every time you run an action in your controller you will most likely open a new context do your changes and save changes. I don't see a scenario where you don't save after editing anything in your context, again a little more detail would help.

synepis
  • 1,292
  • 16
  • 28
0

You're looking for:

Complex model binding to a list

Community
  • 1
  • 1
John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • Ok, I took a look at that but it really wasn't what I was after. I kept digging and finally figured out the problems I was having. I ended up having to add a ProductID property to the OrderItem class so I could set that instead of trying to reassign a full Product. – Richard Edwards Dec 10 '10 at 20:33