【问题标题】:MVC LINQ query to populate model with a one-to-many and many-to-one tablesMVC LINQ 查询以使用一对多和多对一表填充模型
【发布时间】:2012-11-15 21:42:21
【问题描述】:

在给定域表结构的情况下,我一直在尝试填充我的模型。

我有一个存储供应商的主表,每个供应商都可以从主类别查找表中选择一对多的类别。供应商选择存储在另一个表中,该表仅存储 VendorID 和 CategoryID 以在它们之间进行链接。

我可以编写查询(如下)以包含类别表,但我只能显示 categoryID 而不是类别名称。

        public VendorProfile GetVendor(String id)
    {
        Guid pid = Guid.Parse(id);
        var view = _db.VendorProfiles.Include("VendorCategories").Single(v => v.ProfileID == pid);
        return view;
    }

我试图在查询中包含查找表(如下),但这会产生运行时错误。

        public VendorProfile GetVendor(String id)
    {
        Guid pid = Guid.Parse(id);
        var view = _db.VendorProfiles.Include("VendorCategories").Include("ProductServiceCategory").Single(v => v.ProfileID == pid);
        return view;
    }

指定的包含路径无效。 EntityType 'VendorProfilesIntranet.VendorProfile' 没有声明名为 'ProductServiceCategory' 的导航属性。

类别表确实具有导航属性。我不知道如何将相同的导航属性添加到主表,因为它对查找表没有任何 FK。

更新:

@Gert 这个符号确实有效!

_db.VendorProfiles.Include("VendorCategories.ProductServiceCategory").Single(v => v.ProfileID == pid);

但是,我现在显示的只是选择的类别项目。我希望获得完整的类别列表并检查已选择的类别。我正在使用滚动 CheckboxList。

       <div class="scroll_checkboxes">
        <ul>
        @foreach (var c in Model.VendorCategories)
        {
           <li>
               <input type="checkbox" name="categories" value="@c.ID" /> @c.ProductServiceCategory.CategoryName
           </li>
        }
        </ul>
        @Html.ValidationMessage("Please check at least one Product/Service category")
    </div>

更新2:

可能有更好的解决方案,但对于遇到类似情况的人来说,这可行

        <div class="scroll_checkboxes">
        <ul>
        @foreach (var c in ViewBag.Categories)
        {
           <li>
               @foreach(var vc in Model.VendorCategories)
               {
                   if(c.Id == vc.CategoryID)
                   {
                        <input type="checkbox" name="categories" checked="checked" value="@c.Id" /> @vc.ProductServiceCategory.CategoryName
                       <br />
                   }
                   else
                   {
                        <input type="checkbox" name="categories" value="@c.Id" /> @vc.ProductServiceCategory.CategoryName
                        <br />
                   }
               }
           </li>
        }
        </ul>
        @Html.ValidationMessage("Please check at least one Product/Service category")
    </div>

【问题讨论】:

    标签: linq asp.net-mvc-4 entity-framework-5


    【解决方案1】:

    你可以的

    _db.VendorProfiles.Include("VendorCategories.ProductServiceCategory")
    

    在结果集中同时包含VendorCategories 及其ProductServiceCategorys。

    顺便说一句,如果你使用DbExtensions.Include,你有智能感知来帮助你找到正确的包含路径。

    【讨论】:

    • 天哪,有效。我以前从未见过这种符号。谢谢你。但是,我现在显示的只是选择的类别项目。我希望获得完整的类别列表并检查已选择的类别。我正在使用 CheckboxList 请参阅上面的更新
    • 我认为您最好使用包含VendorProfile 和所有类别的视图模型。客户端(即 cshtml)可以通过匹配 id 或名称来设置所选类别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多