开发者

In ASP.NET MVC, How do I synchronise or combine two collections of different types

I have two collections, OrderItems and Items. The reason there are two is because in my model I have an Item which is fixed, and an OrderItem which relates to an Item and adds order specific information such as quantity and a property (OrderID) that relates back to my Order object.

In Entity Framework 4, in order to have a collection of objects in a model you need to relate back to that model in the collection type's object. This makes the OrderID property in Item necessary.

Here are the POCO's in code:

public class Order {
    public int OrderID { get; set; }
    public DateTime DatePlaced { get; set; }
    public bool Filled { get; set; }
    public string Comment { get; set; }

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

public class OrderItem {
    public int OrderItemID { get; set; }
    public int OrderID { get; set; }
    public int Quantity { get; set; }
    public int ItemID { get; set; }
}

public class Item {
    public int ItemID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Size { get; set; }
}

So now I have my models, and I want to display an Order Details page. I can send over the Order to the ViewModel (MVC 3) or ViewData (MVC 2/1) from the controller which works fine, and I can send a collection of Item as well, but how do I display a list of Items that contain both the Item properties and the Quantity from the OrderItem? Since both are collections, I could OrderBy ItemID and loop over both at the same time in the view, but that seems reall开发者_如何学JAVAy messy.

I fiddled with creating an anonymous class that combined both Items and Order but that didn't really work when it came to combining the collections.

I basically want to loop through each Item and OrderItem and display the following properties, without doing it in the view:

Name

Description

Size

Quantity

Ideas?


I believe, forgive me if I'm wrong, but with EntityFramework (assuming code first, but I think db first you can still do it) you can set the following:

public class OrderItem {
    public int OrderItemID { get; set; }
    public int OrderID { get; set; }
    public int Quantity { get; set; }
    public virtual Item Item { get; set; }
}

And then when you refer to Item.Name etc. it will lazily load the data in. I don't have access at the moment to test.

Hope this helps, or at least guides you in the right direction.

Edit

Thinking about it, I believe if you're doing db first then as long as you've got the relationship defined in the database then you should be able to access the associated Item from the OrderItem through the relationship property.


Is Item not supposed to derive from OrderItem and you only show Item which has all info for OrderItem and a bit more?

So you will only get the items from database and display using templates, ...

public class OrderItem {
    public int OrderItemID { get; set; }
    public int OrderID { get; set; }
    public int Quantity { get; set; }
    public int ItemID { get; set; }
}

public class Item : OrderItem {
    // public int ItemID { get; set; } not anymore needed
    public string Name { get; set; }
    public string Description { get; set; }
    public int Size { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜