ASP.NET MVC读取控制器的其他实体

时间:2012-05-18 20:39:53

标签: asp.net-mvc entity-framework

我有2个实体订单和项目与OrdersController和ItemsController。 在我的订单索引视图中,我有一个webgrid

grid.Column(columnName: "OrderNo", header: "Order No.", canSort: true),
grid.Column(columnName: "OrderDate", header: "Order Date", canSort: true),                                                                                        
grid.Column(" ", " ", format: @<a href="@Url.Action("Index", "Items", new { id = item.OrderNo })">Items</a>)

在ItemsController中,我添加了索引参数:

public ActionResult Index(string id = "0")
    {
        var orderq = from a in db.items
                        where a.OrderNo== id
                        select a;
        return View(orderq.ToList()); // ex : orderq.Count = 2
    }...
    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    } // Object reference not set to an instance of an object

但是我将 Object引用设置为对象的实例。

问:我需要从订单中打开商品视图?

我是否必须清除我的背景并再次声明它?

1 个答案:

答案 0 :(得分:0)

Order.cshtml

grid.Column(" ", " ", format: @<a href="@Url.Action("Index", "Items", new { id = item.OrderNo})">Items</a>)));

ItemsController.cs

public ActionResult Index(string id = "0")
    {
        var itemsqry = db.items.Where(a=>a.OrderNo == id);
        return View(itemsqry .ToList());
    }