0
CategoryName我如何可以顯示導航屬性的值在asp.net mvc的
指數方法:
public ActionResult Index(int? page)
{
var data = objContext.Products.ToList().ToPagedList(page ?? 1, 1);
return View(data);
}
CategoryName我如何可以顯示導航屬性的值在asp.net mvc的
指數方法:
public ActionResult Index(int? page)
{
var data = objContext.Products.ToList().ToPagedList(page ?? 1, 1);
return View(data);
}
如果您product
POCO,你有Category
導航屬性Product
,簡單使用:
@Html.DisplayNameFor(modelItem => model.Category.Title)
假設你的產品和類別如下:
public class Product {
public int ID { get; set; }
public string ProductName { get; set; }
public int CategoryId { get; set; }
// and other properties
public virtual ProductCategory ProductCategory { get; set; }
}
public class ProductCategory {
public int ID { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
,並在地圖中定義product
您有:
internal class ProductMap : EntityTypeConfiguration<Product> {
public ProductMap() {
// Primary Key
this.HasKey(t => t.ID);
// Properties, for example
this.Property(t => t.ProductName)
.HasMaxLength(200);
// Table Mappings
this.ToTable("Product");
//Relationships
this.HasRequired(t => t.ProductCategory)
.WithMany(t => t.Products)
.HasForeignKey(d => d.CategoryId);
}
}
添加你的'product'類POCO。 –