【问题标题】:bind menu and submenu from database in asp net core entity framework mvc在asp net core entity framework mvc中从数据库绑定菜单和子菜单
【发布时间】:2017-06-02 23:30:43
【问题描述】:

我在数据库行业和类别中有 2 个表。他们使用 IndustryId 作为表中的主键和外键加入。现在我想将 Industry 作为我的主菜单项,将相关类别显示为我各自行业的子菜单项。我在 Visual Studio 2017 中使用 asp net mvc enity 框架作为我的编码架构。这是我在这个架构上的第一个应用程序我想要一个简要的解释,包括模型视图和控制器一步一步。我之前已经在 asp net web 表单上完成了这个绑定没有mvc。但是实体框架似乎与以前的版本完全不同。

【问题讨论】:

    标签: entity-framework asp.net-core-mvc


    【解决方案1】:

    编辑基于用户评论,他想在加载时绑定数据,制作嵌套列表,我会留下我的旧答案,因为它可能对其他人有帮助,我也会添加新答案, 我的建议是您将行业列表作为 ViewBag 传递,因为类别中有与行业表相关的行业 ID 的外键,ef 将检测该关系并在您的行业模型中将其表示为 virtual Collection<Category> Categories,这表示与相关的所有类别这个行业。 你可以做的是通过它然后使用剃刀语法在你的视图中做这样的事情 在您的控制器中,您将其作为 viewbag 传递

     ViewBag.Industries = dbContext.Industries.Include(x => x.Categories).ToList();
    

    那么在你看来

        <!-- init your list -->
                    <ul>
        <!-- loop through your viewbag -->
                    foreach (var x in (List<Industry>) ViewBag.Industries)
                    {
                      <li> x.IndustryName
        <!-- init your sub menu -->               
        <ul>
        <!-- loop through your categories of this industry x -->
        foreach(var c in x.Categories)
        { 
        <li> c.CategoryName </li>
        } 
                      </ul>
                      </li>
                    }
    <!-- loop end, close your list -->
    </ul>
    

    对于希望根据另一个菜单值动态更新菜单内容的用户,请阅读以下内容:

    我解决这个问题的方法是,首先创建接受行业 id 并返回列表或类别的操作,每当我的行业 id 列表在前端更改时(更改事件侦听器),我使用 ajax 调用此操作,然后获取作为对象列表返回,使用 jquery 将数据绑定到我的子菜单。 像

    public ActionResult GetCategories(int IndustryID)
    {
    // this simple select query, retrieves all categories that have industry id provided 
    var categories = dbContext.Categories.Where(c => c.IndustryId == IndustryID).ToList();
    return Json(categories);
    }
    

    ajax 代码

     // industryId is the id of industry in your view, apply change event listener
    $('#industryId').change(function () {
              // ajax call, get data
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("GetCategories", "controllerhere")',
                    data: { IndustryID: this.value},
                    dataType: "json"
                }).done(function(data){
                      // Data recieved, get categories list, empty it, bind the new data 
                       var ddl = $("#categoriesList");
                    ddl.empty();
                    ddl.append($('<option>', {value: 0,text: 'Select'})); //Newly added
                   // categoryID and CategoryName are properties in your category, these Names I assumed, they might change depending on your Model 
                    $.each(data, function () {
                        ddl.append($('<option></option>').attr("value", this.CategoryID).text(this.CategoryName));
                    });
                });
            });
    

    我希望这回答了你的问题,我不确定你所说的实体框架不同是什么意思,你可以问我更具体的问题也许我可以帮助

    【讨论】:

    • 感谢您的建议,但我想在我的页面顶部的菜单(导航栏)上绑定这些行业和类别,而不是在任何下拉或单击事件上。所以您能提供动态绑定的解决方案吗导航栏上的导航项。其余所有问题保持不变
    • 你可以传递一个行业列表,每个行业都会有一个类别列表,然后你使用Razor语法,可以循环遍历行业列表,输出想要的html。
    • 它有效。希望在同样的问题上也能帮助其他人。
    • 如果成功请标记为正确答案,我很高兴能帮上忙
    • 你的答案是正确的,我也标记了。但是,如果我想将导航栏绑定到共享布局页面怎么办。它只会在我定义了在各个控制器中绑定功能的页面中绑定。但这会增加代码行数。我尝试了局部视图但没有成功。所以指导我正确完成它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多