的單個實現Q:如何在Order對象中訪問以下OrderStatusType (在我的控制器和剃刀視圖中使用mvc3/ef4.1/.edmx)?以下代碼將引發標題中指定的錯誤。錯誤:類型'MVCApp.Models.Order'的導航屬性不是'System.Collections.Generic.ICollection`1 [T]'
- 控制器代碼(錯誤):
public ViewResult Details(int id)
{
Order o = db.Orders.Find(id);
OrderStatusType os = o.OrderStatusType; // <= This is throwing!
return View(o);
}
- 型號:
public class Order
{
public int OrderId { get; set; }
public int Desc { get; set; }
public int OrderStatusTypeId { get; set; }
public virtual OrderStatusType OrderStatusType { get; set; } // Order contains an OrderSTatusType
}
public class OrderStatusType
{
public int OrderStatusTypeId { get; set; }
public int Name { get; set; }
}
- 剃鬚刀(錯誤):
<div class="display-field">
@Model.OrderStatusType.Name @* This throws the same error as well *@
</div>