0
他們我有兩個職業類別和產品1對多的關係 這裏是我的範疇類兩個表之間的加入,並顯示在我的網頁
public class Category
{
public int ID { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public ICollection<Product> Products { get; set; }
}
這是我的產品類別(從羅斯文複製)
public class Product
{
public int ID { get; set; }
[Required(ErrorMessage ="*")]
public String Name { get; set; }
[Required(ErrorMessage = "*")]
public int QuantityPerUnit { get; set; }
[Required(ErrorMessage = "*")]
public decimal UnitPrice { get; set; }
[Required(ErrorMessage ="*")]
public int UnitsInStock { get; set; }
[Required(ErrorMessage ="*")]
public int UnitsOnOrder { get; set; }
[Required(ErrorMessage ="*")]
public int ReorderLevel { get; set; }
[Required(ErrorMessage ="*")]
public int CategoryID { get; set; }
}
而且我有一個名爲ProductsController的一個控制器,它有)調用指數(視圖
public ActionResult Index()
{
var ProductList = from p in db.Products
join c in db.Categories.ToList()
on p.CategoryID equals c.ID
select new {ID = p.ID, Name = p.Name, QuantityPerUnit = p.QuantityPerUnit,
ReorderLevel = p.ReorderLevel, UnitPrice = p.UnitPrice,
UnitsInStock = p.UnitsInStock, UnitsOnOrder = p.UnitsOnOrder, Category = c.Name };
return View(ProductList.ToList());
}
,這裏是由實體Framrwork爲索引視圖生成的代碼
<span>@model IEnumerable<BootstrapSite1.Models.Product></span>
我的看法讀取產品的集合
@Html.DisplayNameFor(model => model.Name)
@Html.DisplayNameFor(model => model.QuantityPerUnit)
@Html.DisplayNameFor(model => model.UnitPrice)
@Html.DisplayNameFor(model => model.UnitsInStock)
@Html.DisplayNameFor(model => model.UnitsOnOrder)
@Html.DisplayNameFor(model => model.ReorderLevel)
@Html.DisplayNameFor(model => model.CategoryID)
我的問題是,你可以看到我要讓之間的連接產品類別和類別類別中的CategoryID並替換類別ID我們類別名稱,但問題是產品對象只接受一個Int而不是一個字符串,所以我如何解決這個問題提前感謝。