0

ユーザーが製品のカスタムフィールドを持つことを可能にする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.PropertyNamePropertyValuePropertyName

@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>    
}
4

2 に答える 2

1

このモデルをビューに渡す方法を示すコードを追加する必要がありますが、Lazy Loading が原因だと思います。コントローラーで評価しないDataType仮想プロパティがあるため、データベースから読み込まれません...Include積極的な読み込みのためにLINQクエリでメソッドを使用します

于 2013-06-18T06:50:03.897 に答える
0

モデルがビューに渡される前に、プロパティが読み込まれていることを確認してくださいDataType(遅延読み込みの場合、そうではない可能性があります)。

ORMを使用していますか?最初に参加する必要があるかもしれません - 使用しているプロバイダーのタイプについて詳しく教えていただければ、サンプル コードを提供できるはずです。

于 2013-06-18T06:52:28.557 に答える