Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Menu in C# ASP.Net MVC3 Razor

Explaining

I'm trying to do a dynamic menu, loading items from database. I need 3 levels max inside the menu, like this:

<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

Now, this is how i'm doing today, but without sucess:

The partial view "TopBar.cshtml" shows in every page, and it's called inside "_Layout.cshtml" like this:

_Layout.cshtml

<body>
    @Html.Partial("TopBar")
    <div class="container body-content">
        @RenderBody()
        (...)

and the "TopBar.cshtml" shows the data using the code below

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

This is the "TopMenu" class

    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
    }

All this result in blank list, like a clean database. But, when i call an action List e.g. a conflict occurs, because booth Views ("List.cshtml" and "TopBar.cshtml") starts with:

@model IEnumerable<SIGO.Models.Employee>

or

@model IEnumerable<SIGO.Models.TopMenu>

P.S.: I don't use any controller to process the data to TopMenu.

Questions

  • How can i do this TopMenu?
  • Do you have another solution?

Thanks! Sorry for any mistake in translation

like image 314
Anpix Avatar asked Jul 23 '14 23:07

Anpix


People also ask

What is menu driven in C?

In a menu driven program, the user is given a set of choices of things to do (the menu) and then is asked to select a menu item. The driver then calls an appropriate function to perform the task selected by the menu item. A switch statement seems a natural one for handling the selection from the menu.

Do loops in C?

do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.


1 Answers

This is the solution reached with the answers above

Thanks for all

Class: 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
    }

Context: SigoContext.cs

    public class SigoContext : DbContext {
        public SigoContext() : base("SigoMain") {}
            public DbSet<TopMenu> TopMenu{ get; set; }
        }
    }

Controller: SigoController.cs

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

        [ChildActionOnly]
        public ActionResult TopMenu() {
            return PartialView("TopBar",db.TopMenu);
        }
    }

Layout: _Layout.cshtml

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

Partial View: 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>

Thank guys!

like image 126
Anpix Avatar answered Sep 16 '22 13:09

Anpix