我有一個CustomProductProperty模式,讓用戶擁有自己的產品自定義字段,內包含鏈接到處理用戶定義的數據類型數據庫CustomDataType模型。的自定義數據類型的類不顯示
[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>
}
如何強制它包括如果控制器正在查找產品? 'DataType'在'IEnumberable
ChaoticLoki
包含方法的用法在此處描述:http://msdn.microsoft.com/en-us/data/jj574232.aspx。如果你向我們展示你用來檢索數據的查詢會更容易... – tdragon
那麼最初我所做的只是'dbcontext.Products.Find(id)' – ChaoticLoki