0

I am loading menus dynamically in the layout page from the database. So, I used Partial View to achieve this. This is my code:

I have created a Models class called "MenuModel":

MenuModel.cs

public class MenuModel
{
    public List<MainMenu> MainMenuModel { get; set; }        
}

public class MainMenu
{
    public int MainMenuID;
    public string MainMenuName;
    public string MainMenuController;
    public string MainMenuAction;
}

I have created a controller class called "Partial":

PartialController.cs

 using System.Data.Entity;
 using Insights.Models;

 public ActionResult Index()
 {
        return View();
 }

 [ChildActionOnly]
 public ActionResult LoadMenu()
 {
        MenuModel ObjMenuModel = new MenuModel();
        ObjMenuModel.MainMenuModel = new List<MainMenu>();
        ObjMenuModel.MainMenuModel = GetMainMenu();
        return PartialView(ObjMenuModel);
 }

 public List<MainMenu> GetMainMenu()
 {
        List<MainMenu> ObjMainMenu = new List<MainMenu>();

        var context = new InsightsEntities();

        ObjMainMenu = context.Insights_mMenu.Where(m => m.parentMenuUID == 0).Select(x => new MainMenu()
        {
            MainMenuID = x.menuUID,
            MainMenuName = x.menuName,
            MainMenuController = x.menuURLController,
            MainMenuAction = x.menuURLAction
        }).ToList();

        return ObjMainMenu;
 }

I have created a PartialView for the ActionResult "LoadMenu":

@model Insights.Models.MenuModel

@{
    foreach (var MenuItem in Model.MainMenuModel)
    {

    <li class="active">
        <div class="sidebar-menu-item-wrapper">
            <a href='@Url.Action(@MenuItem.MainMenuAction, @MenuItem.MainMenuController)'><span>@MenuItem.MainMenuName</span></a>
        </div>
    </li>
    }
}

This is my layout page:

<ul class="sidebar-menu on-click" id="main-menu">
     <li class="active">
           <div class="sidebar-menu-item-wrapper">
               <a href="#">
                   <i class="icon-home"></i>
                   <span>Home</span>
               </a>
           </div>
     </li>

     @{ Html.RenderAction("LoadMenu", "Partial"); }
</ul>

enter image description here The database returns two menus such as "Masters" and "Invoice". But when I click the "Masters" Menu, it again calls the partial controller class and once again the two menus Masters & Invoice are loading again. So I cannot highlight which menu is now opened. I have attached the Masters screen.

thevan
  • 10,052
  • 53
  • 137
  • 202
  • Probably not related, but you are generating invalid html - you have a `
      ` as a child of `
        ` (only `
      • ` elements are valid) and you also have duplicate `id` attributes (both `
          ` elements have `id="main-menu"`. Suggest you remove the `
            ` element from the partial
    –  Oct 30 '15 at 07:27
  • ok i try this and let you know.. – thevan Oct 30 '15 at 07:29
  • @StephenMuecke: Thanks. Its loading now. But when I click the menu, its again calling the LoadMenu method and once again loading all the menus and then moves to the corresponding view. Why its happening? – thevan Oct 30 '15 at 07:37
  • Not sure I understand what your saying. If you click on one of the links it should redirect to the appropriate method and assuming that view uses the same layout, it will call `LoadMenu()` and display the menus again in the new view. –  Oct 30 '15 at 07:46
  • Although something seems odd here. Your layout page should not have `@model Insights.Models.MenuModel` Are you sure that's a layout, or its just a view? –  Oct 30 '15 at 07:49
  • @StephenMuecke: Yes.. The redirected page also uses the same layout page. It again calls the partial controller class and loads the menu again. But It should happen only once during the first time. How to achieve this? – thevan Oct 30 '15 at 07:51
  • Not sure what you mean? How would the menu appear when you redirect if it does not call the `LoadMenu()` method again. Or do you only want this menu on the Home page? –  Oct 30 '15 at 07:53
  • @StephenMuecke: I removed that namespace. Actually, I have two menus Master & Invoice. When I click Master, it redirects to one view and I highlights the Master Menu(to indicate it as open). But when I click Master menu, it again loading both the menus Master & Invoice and redirects to the view which belongs to Master. I hope that I have conveyed the exact problem. – thevan Oct 30 '15 at 07:59
  • Would need to see more code to understand (I don't even know which of those 2 menus is loaded by your `LoadMenu()` method or how you are loading the other one) –  Oct 30 '15 at 08:04
  • Ok. I will edit my question.. Please look at it.. – thevan Oct 30 '15 at 08:06
  • @StephenMuecke: Hi, Please gothrough the screen.. – thevan Oct 30 '15 at 08:26
  • Suggestion: unrelated to your problem but I would avoid using name Partial for controller and even in the name of the view. – Mitul Oct 30 '15 at 14:23

0 Answers0