未显示自定义数据类型类
本文关键字:定义数据类型 显示 | 更新日期: 2023-09-27 18:19:00
我有一个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
虚拟属性,你没有在控制器中评估,所以它没有从数据库加载…在LINQ查询中使用Include
方法来快速加载
确保在模型传递给视图之前加载DataType
属性(如果是惰性加载,则可能不是这种情况)。
是否使用ORM?你可能需要先加入它——如果你提供更多关于你正在使用的提供商类型的细节,我们应该能够提供示例代码。