2

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?

dub stylee
  • 3,252
  • 5
  • 38
  • 59
  • 2
    if you want to use Razor, create a PartialView and make LoadMenus an action result and `return PartialView("_Menu",menus);` in your Partial View you can loop thru your `@model IEnumerable` and build your menu. Just place `@Hmtl.Action("LoadMenus")` where you want the menu – JamieD77 Jun 20 '16 at 18:22
  • Ah, I think that the `@model` bit may have been what I am looking for. I have been able to use a partial view for the menu, but was still unsure how to access the information from within the view. – dub stylee Jun 20 '16 at 18:25
  • I was also able to generate the links and specify the controller by using `Url.Action()` like so: `$"{menu.MenuName}
    "`
    – dub stylee Jun 20 '16 at 18:31

1 Answers1

7

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

dub stylee
  • 3,252
  • 5
  • 38
  • 59
  • I am able to get the menu loaded in the partial view as described in your answer, however, after the first click of an item, the `Menu.Items` becomes `null` and the site breaks when it tries to render the new page. – dub stylee Jun 20 '16 at 22:10