我有一个CustomProductProperty模型,它允许用户为其产品拥有自定义字段,其中包含一个CustomDataType模型,该模型链接到处理用户定义的 DataTypes 的数据库。
[Table("Product")]
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductID { get; set; }
public int ProductTypeID { get; set; }
[Display(Name = "Product Name")]
public string ProductName { get; set; }
[ForeignKey("ProductTypeID")]
[Display(Name = "Product Type")]
public virtual ProductType ProductType { get; set; }
public virtual ICollection<CustomProductProperty> CustomProperties { get; set; }
}
[Table("CustomProductProperty")]
public class CustomProductProperty
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomPropertyID { get; set; }
public int CustomDataTypeID { get; set; }
[ForeignKey("CustomDataTypeID")]
public virtual CustomDataType DataType { get; set; }
public int ProductID { get; set; }
[ForeignKey("ProductID")]
public virtual Product Product { get; set; }
public string PropertyValue { get; set; }
}
[Table("CustomDataType")]
public class CustomDataType
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomTypeID { get; set; }
public string PropertyName { get; set; }
public CustomDataTypes DataType { get; set; }
public int ModuleID { get; set; }
[ForeignKey("ModuleID")]
public Module Module { get; set; }
}
当试图在我的视图中显示它时,我创建了一个显示模板来处理CustomProductProperty的ICollection<>出于 某种原因,它是空的,但仍然返回正确的数据,有人知道为什么是空的吗?DataType.PropertyName
PropertyValue
PropertyName
@model IEnumerable<InventoryManager.Models.CustomProductProperty>
@{
Layout = null;
}
@foreach (var item in Model)
{
<tr>
<th>@Html.DisplayFor(model => item.DataType.PropertyName)</th>
<td>@Html.DisplayFor(model => item.PropertyValue)</td>
</tr>
}