title: Dynamic Menu loading in ASP.Net MVC3 Razor
body: <p>I want to ask about Dynamic Database Driven  Menu Loading in ASP.Net </p>
  
  <p>MVC3 razor .  </p>
  
  <p>This is my</p>
  
  <p>1.View Code </p>
  
  <p>2.Model for Menu</p>
  
  <p>3.Model For Menu Items</p>
  
  <p>4.Navigation Model Structure</p>
  
  <p>In this project i have different User Logins .For each login they are </p>
  
  <p>limited by modules .So When a user enter into the app with their </p>
  
  <p>login ,they have only a limited access to their account.</p>
  
  <blockquote>
    <p>For Example,There are two users A and B. User A can access only Accounts and Reports and User B can access only
    Reports .</p>
  </blockquote>
  
  <p>To implement these things am loading menu from Database.</p>
  
  <p><em><strong>My Question is
  How to implement menu Loading from database in ASP.Net MVC3 Razor</em></strong></p>
  
  <p><em><strong>View Code</em></strong></p>
  
  <pre><code>@{
      ViewBag.Title = "Elixir ERP V1.0 Beta";
      Layout = "~/Views/Shared/_LayoutUser.cshtml";
  }
  &lt;div class="main-container"&gt;
      &lt;div class="main-wrapper"&gt;
          &lt;div class="scroll-top"&gt;
              &lt;a href="#" class="tip-top" title="Go Top"&gt;&lt;i 
  
  class="icon-arrow-up"&gt;&lt;/i&gt;&lt;/a&gt;
          &lt;/div&gt;
          &lt;div class="left-bar merge-left"&gt;
              &lt;!-- SEARCH BAR --&gt;
              &lt;!-- LEFT NAV --&gt;
            @section leftnav{
  
  
            }
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="container"&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><em><strong>Menu Model</em></strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace Elixir.Models
  {
      public class Menu
      {
          public Menu()
          {
              MenuItems = new List&lt;MenuItem&gt;();
          }
  
          public int Id { get; set; }
          public string Name { get; set; }
          public List&lt;MenuItem&gt; MenuItems { get; set; }
      }
  }
  </code></pre>
  
  <p><em><strong>Menu Items</em></strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace Elixir.Models
  {
  public class MenuItem
  {
  
  public int Id { get; set; }
  public int UserName { get; set; }
  public string Name { get; set; }
  public string ActionName { get; set; }
  public string ControllerName { get; set; }
  public string Url { get; set; }
  public Menu ParentMenu { get; set; }
  }
  }
  </code></pre>
  
  <p><em><strong>Navigation Model Structure</em></strong></p>
  
  <pre><code> &lt;div class="left-nav"&gt;
  
  
                  &lt;ul class="side-navigation accordion" 
  
  id="nav-accordion"&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-home"&gt;&lt;/i&gt;Home&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i class="icon-list-alt"&gt;&lt;/i&gt;User 
  
  Management&lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Create New User&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Manage Users&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;View Users&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-table-2"&gt;&lt;/i&gt;Employee Management&lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Employee Registration&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Manage Employees&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;View Employees&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i class="icon-columns"&gt;&lt;/i&gt;DMIT 
  
  Management&lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Scan Management&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Report Generation&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-laptop"&gt;&lt;/i&gt;Franchise Management &lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Franchise Registration&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Franchise Fee Management&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Franchise Account 
  
  Management&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i class="icon-font"&gt;&lt;/i&gt;Customer 
  
  Management &lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Customer Registration&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Manage Customer&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-cord"&gt;&lt;/i&gt;Counseling Management&lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Add Counseling Session&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Manage Counseling 
  
  Sessions&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-tools"&gt;&lt;/i&gt;Accounts&lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Manage Income&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Manage Expense&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-files"&gt;&lt;/i&gt;Reports&lt;/a&gt;
                          &lt;ul&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Elxir Monthly Reports&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Elixir Yearly Reports&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Franchise Monthly 
  
  Reports&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;&lt;i 
  
  class="icon-double-angle-right"&gt;&lt;/i&gt;Franchise yearly Reports&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
  </code></pre>