我有一個小問題,不知道如何解決它。ASP NET MVC包括雙我的看法
當我在我的視圖和控制器寫:
@Html.DisplayFor(modelItem => item.Category.Name)
return View(db.Product.Include(p=>p.Category).OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid).ToList());
當我將其更改爲:
@Html.DisplayFor(modelItem => item.CategoryID)
return View(db.Product.OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid).ToList());
所以一切正常,但它不顯示類別名稱。我該如何解決它?
EDIT1:
public partial class Product
{
public int ID { get; set; }
public int ProductID { get; set; }
public string Name { get; set; }
public int CategoryID { get; set; }
public decimal Price { get; set; }
public int Promotion { get; set; }
public string Image1 { get; set; }
public string Image2 { get; set; }
public string Image3 { get; set; }
public string Image4 { get; set; }
public string Description { get; set; }
public int ClientID { get; set; }
public virtual Category Category { get; set; }
public virtual Client Client { get; set; }
}
在OrderByDescending上獲取此錯誤: 'IGrouping'不包含'ProductID'的定義並且沒有找到接受類型爲'IGrouping '的第一個參數的擴展方法'ProductID'(您是否缺少using指令或程序集引用?) –
Ashiv3r
我更新了答案 – Hadee
說明:在處理期間出現未處理的異常執行當前的Web請求。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.InvalidOperationException:傳入字典的模型項類型爲'System.Collections.Generic.List'1 [System.Linq.IGrouping'2 [System.Int32,AMBIT_CMS_MVC.Areas.Admin.Models。 Product]]',但是這個字典需要一個'System.Collections.Generic.IEnumerable'1 [AMBIT_CMS_MVC.Areas.Admin.Models.Product]'類型的模型項。 – Ashiv3r