Dynamic menus from database in MVC

17,078

You should keep your html in the cshtml views.

You should pass the data through the viewmodel and not through the session.

1)

In the controller, get the menu data (in this example we fetch some fake data).

Create a viewmodel that can hold the menu data and pass it to the view, as shown below:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var menu = GetMenu();
        var vm = new ViewModel() {Menu = menu};
        return View(vm);
    }
    private Menu GetMenu()
    {
        var menu = new Menu();
        var menuItems = new List<MenuItem>();
        menuItems.Add(new MenuItem() { LinkText = "Home" , ActionName = "Index", ControllerName = "Home"});
        menuItems.Add(new MenuItem() { LinkText = "About", ActionName = "About", ControllerName = "Home" });
        menuItems.Add(new MenuItem() { LinkText = "Help", ActionName = "Help", ControllerName = "Home" });
        menu.Items = menuItems;

        return menu;
    }
}

2)

This is the viewmodel

public class ViewModel
{
    public Menu Menu { get; set; }
}

This view is an example of how you could render the menu data as a html menu

@model WebApplication1.Models.ViewModel

<ul id="menu">
    @foreach (var item in @Model.Menu.Items)
    {
        <li>@Html.ActionLink(@item.LinkText, @item.ActionName,
              @item.ControllerName)</li>
      }
</ul>

3)

This is the example menu classes used (representing your entities from the dbcontext)

public class Menu
{
    public List<MenuItem> Items { get; set; }
}

public class MenuItem
{
    public string LinkText { get; set; }
    public string ActionName { get; set; }
    public string ControllerName { get; set; }
}

Here are some links to get you started:

http://www.codeproject.com/Articles/585873/Basic-Understanding-On-ASP-NET-MVC http://www.asp.net/mvc/overview/getting-started/introduction/getting-started

Share:
17,078
dub stylee
Author by

dub stylee

A programmer with an undying love for music of all varieties. SOreadytohelp

Updated on June 04, 2022

Comments

  • dub stylee
    dub stylee almost 2 years

    I have read some similar topics here and on the web, but I don't think I have seen one that would classify this as a duplicate, so I am going to go ahead and post it. I am currently loading my dynamic menus from the database like so:

    public void LoadMenus()
    {
        var dbContext = new ContentClassesDataContext();
        var menus = from m in dbContext.Menus
                    where m.MenuName != "Home" && m.MenuGroup == "RazorHome" && m.RoleID == "Facility"
                    orderby m.Sequence, m.MenuName
                    select m;
    
        var html = "";
    
        if (menus.Any())
        {
            html += "<span/>";
    
            foreach (var menu in menus)
            {
                html = html + $"<a href='{menu.URL}'>{menu.MenuName}</a><br/>";
            }
            html += "<hr>";
        }
    
        Session["Menus"] = html;
    }
    

    LoadMenus() is in my controller class, so I am not able (to my knowledge) to use Razor syntax. I would prefer to load the menus from the view instead, so that I am able to use @Html.ActionLink(linkText, actionName, controllerName). Loading the HTML the way I am currently doing it will generate different link text depending on the current controller, so the links are not always correctly rendered. Is it possible to access the database from the view? Or perhaps to just pass in the content from the database from the controller to the view and then render the menu that way?