C#ASP.Net MVC3 Razor中的动态菜单

Anp*_*pix 5 c# asp.net-mvc razor

解释

我正在尝试做一个动态菜单,从数据库加载项目.我在菜单中最多需要3个级别,如下所示:

<ul>
   <li>Home</li>
   <li>Peoples
      <ul>
         <li>Employee
            <ul>
              <li>Create</li>
              <li>List</li>
              <li>Edit</li>
            </ul>
         </li>
         <li>Training</li>
         <li>Material Requisition</li>
      </ul>
   </li>
</ul
Run Code Online (Sandbox Code Playgroud)

现在,这就是我今天的表现,但没有成功:

部分视图"TopBar.cshtml"显示在每个页面中,并在"_Layout.cshtml"内部调用,如下所示:

_Layout.cshtml

<body>
    @Html.Partial("TopBar")
    <div class="container body-content">
        @RenderBody()
        (...)
Run Code Online (Sandbox Code Playgroud)

并且" TopBar.cshtml "使用下面的代码显示数据

@model IEnumerable<SIGO.Models.TopMenu>
<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <div class="SigoLogo" onclick="location.href='@Url.Action("")'">
                <a href="@Url.Action("Index", "Home")" title="Início">
                    <img src="~/Content/images/Wlogo.png" />
                </a>
            </div>
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"></button>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                @if (Model != null){
                    foreach(var item in Model.Where(p => p.Nivel == 0)) {
                        if (Model.Where(s1 => s1.Parent == item.TopMenuID) != null) {
                            <li>@item.Descricao
                                <ul>
                                    @foreach (var sub1 in Model.Where(s1 => s1.Parent == item.TopMenuID)) {
                                        if (Model.Where(s2 => s2.Parent == sub1.TopMenuID) != null) {
                                            <li>@sub1.Descricao
                                                <ul>
                                                    @foreach (var sub2 in Model.Where(s2 => s2.Parent == sub1.TopMenuID)) {
                                                    <li>@Html.ActionLink(sub2.Descricao,sub2.Action,sub2.Controller)</li>
                                                    }
                                                </ul>
                                            </li>
                                        }else{
                                            <li>@Html.ActionLink(sub1.Descricao,sub1.Action,sub1.Controller)</li>
                                        }
                                    }
                                </ul>
                            </li>
                        }else{
                            <li>@Html.ActionLink(item.Descricao,item.Action,item.Controller)</li>
                        }
                    }
                }
            </ul>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是" TopMenu "课程

    public class TopMenuItem {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int Id { get; set; }             //Iterator
            public int Parent { get; set; }         //TopMenuItem parent id
            public bool Group { get; set; }         //If this have another item below
            public string Descricao { get; set; }   //Text to show
            public string Action { get; set; }      //Action to Go
            public string Controller { get; set; }  //Controller to Go
    }
Run Code Online (Sandbox Code Playgroud)

所有这些都导致空白列表,就像一个干净的数据库.但是,当我调用动作列表时,例如发生冲突,因为booth Views("List.cshtml"和"TopBar.cshtml")以以下内容开头:

@model IEnumerable<SIGO.Models.Employee>
Run Code Online (Sandbox Code Playgroud)

要么

@model IEnumerable<SIGO.Models.TopMenu>
Run Code Online (Sandbox Code Playgroud)

PS:我没有使用任何控制器来处理TopMenu的数据.

问题

  • 我怎么能做这个TopMenu?
  • 你有其他解决方案吗?

谢谢!对不起翻译中的任何错误

Anp*_*pix 6

这是通过上述答案达到的解决方案

谢谢大家

类:TopMenu.cs

    public class TopMenu {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }             //Iterator
        public int Parent { get; set; }         //TopMenuItem parent id
        public bool Group { get; set; }         //If this have another item below
        public string Descricao { get; set; }   //Text to show
        public string Action { get; set; }      //Action to Go
        public string Controller { get; set; }  //Controller to Go
    }
Run Code Online (Sandbox Code Playgroud)

上下文:SigoContext.cs

    public class SigoContext : DbContext {
        public SigoContext() : base("SigoMain") {}
            public DbSet<TopMenu> TopMenu{ get; set; }
        }
    }
Run Code Online (Sandbox Code Playgroud)

控制器:SigoController.cs

    public class SystemController : Controller {
        private SigoContext db = new SigoContext();

        [ChildActionOnly]
        public ActionResult TopMenu() {
            return PartialView("TopBar",db.TopMenu);
        }
    }
Run Code Online (Sandbox Code Playgroud)

布局:_Layout.cshtml

...
<body>
    @{Html.RenderAction("TopMenu", "System");}
    <div class="container body-content">
        @RenderBody()
...
Run Code Online (Sandbox Code Playgroud)

部分视图:TopMenu.cshtml

@model IEnumerable<SIGO.Models.TopMenu>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <div class="SigoLogo">
                    <a href="@Url.Action("Index", "Home")" title="Início">
                        <img src="~/Content/images/Wlogo.png" />
                    </a>
                </div>
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"></button>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    @if (Model != null){
                        foreach(var item in Model.Where(p => p.Parent == 0)) {
                            if (Model.Where(s1 => s1.Parent == item.Id) != null) {
                                <li>@item.Descricao
                                    <ul>
                                        @foreach (var sub1 in Model.Where(s1 => s1.Parent == item.Id)) {
                                            if (Model.Where(s2 => s2.Parent == sub1.Id) != null) {
                                                <li>@sub1.Descricao
                                                    <ul>
                                                        @foreach (var sub2 in Model.Where(s2 => s2.Parent == sub1.Id)) {
                                                        <li>@Html.ActionLink(sub2.Descricao,sub2.Action,sub2.Controller)</li>
                                                        }
                                                    </ul>
                                                </li>
                                            }else{
                                                <li>@Html.ActionLink(sub1.Descricao,sub1.Action,sub1.Controller)</li>
                                            }
                                        }
                                    </ul>
                                </li>
                            }else{
                                <li>@Html.ActionLink(item.Descricao,item.Action,item.Controller)</li>
                            }
                        }
                    }
                </ul>
            </div>
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

谢谢你们!