0

I have a Login page where on login it takes user to Home Page where dynamic menu is loaded.The problem is that when a user clicks on one of the menulink the loaded menu is not visible

This is because I have written the code inside Index action of theHome controller.

So my question is where should I write the logic for dynamic menu so that it is accessible on clicking the menulink.

_Layout.cshtml file where menu is loaded

 @model SMS.Models.ViewModel.DashboardVM

 @if (Model != null && Model.MenuParentList.Count > 0)
            {
                <!-- Sidebar Menu -->
                <ul class="sidebar-menu">
                    <li class="header">MAIN NAVIGATION</li>
                    <li class="active">
                        <a href="#">
                            <i class="fa fa-dashboard"></i> <span>Dashboard</span>
                        </a>
                    </li>
                    @foreach (var parentItem in Model.MenuParentList)
                    {
                        <li class="treeview">
                            <a href="#">
                                <i class="fa fa-th"></i>
                                <span>@parentItem.MenuParentName</span>
                                <i class="fa fa-angle-left pull-right"></i>
                            </a>
                            <ul class="treeview-menu">
                                @Html.Partial("_MenuParent", Model.MenuList.Where(x => x.ParentID == parentItem.MenuParentID))
                            </ul>
                        </li>
                    }                       

                </ul> 
            }      

Logic for dynamic menu goes here

 public ActionResult Index()
    {


            var _dashboardVM = new DashboardVM
            {
                User = _employee.Users.FirstOrDefault(),

                MenuParentList=_db.Menus
                                 .Where(x => _parentList.Contains(x.Id))
                                 .Select(x => new SMS.Models.ViewModel.DashboardVM.MenuParent
                                 {
                                     MenuParentID = x.Id,
                                     MenuParentName = x.MenuName
                                 })
                                 .OrderBy(x=>x.MenuParentID)
                                 .ToList(),
                MenuList=_employee.Designation.Role.MenuRoles
                                              .Select(x=>x.Menu)
                                              .ToList()

            };
     }
ksg
  • 3,927
  • 7
  • 51
  • 97

1 Answers1

3

Create a separate [ChildActionOnly] method that generates your menu and call it from the layout page so its available in all pages

[ChildActionOnly]
public ActionResult Menu()
{
  var model = new DashboardVM
  {
     ....
  }
  return PartialView("_Menu", model);
}

and create a _Menu.cshtml partial view to generate the html

@model DashboardVM
....

and then in your layout, remove @model SMS.Models.ViewModel.DashboardVM (a layout should not have a model unless that model is a base class for all models used by the layout) and then include

@Html.Action("Menu", yourControllerName)

which will call the Menu method and insert the partial view it returns into the layout.

  • Thanks mate for your wonderful support..I came know more about `ChildAction` from another `stack overflow question` [Using ChildActionOnly in MVC](http://stackoverflow.com/questions/10253769/using-childactiononly-in-mvc).Thanks `StackOverflow` for helping newbies like me. :) – ksg Nov 09 '15 at 08:46
  • @ksg, Can't help with that one. But IMHO, its a poor solution. If you must use Session, then you should always be checking on the server if the Session value exists, and if not, get it from the database again. –  Nov 19 '15 at 00:47
  • I agree with you on that concept , but what is the problem on extending session before it gets timedout . In the end it gives the user a good experience without having going to login page every time the session expires... – ksg Nov 19 '15 at 06:37
  • What do you mean _go to the login page_. You should not be using session for login/authentication information. That should be in the `FormsAuthentication` cookie. –  Nov 19 '15 at 06:39
  • Kindly look into [this question](http://stackoverflow.com/questions/33909005/using-foolproof-requiredif-on-multiplecondition) – ksg Nov 25 '15 at 05:35