Adalat  Khan

Adalat Khan

  • 626
  • 1.5k
  • 846.1k

Problem in Dynamic Menu in MVC Core

Jul 13 2019 12:34 AM
I am creating Main Menu Dynamically when a user log in to the application. I have three user roles such as Administrator, Manager, and User. I assigned specific roles to each user and these righs i have stored in a table name UserRightsDetails. When a User press the login button after entering the user name and password then the user rights details are retrieved from the UserRightsDetails table using linq query in action method of controller then i assigned the query result to the ViewBack and in _Layout.cshtml page i am creating dynamic menu from that ViewBack. When a user login to the application the application Index page of Home Controller is displayed and the main Menu is created in the _Layout.cshtml page. My problem is that when a user is in Index page then the Menu is created and working but when a user navigate the main menu and visited to another View using Menu Items of the Main Menu then another View is displayed but the Main Menu Items become disappear. It means the Main Menu Items are only displayed and the menu is created when a user is in Index page but when a user visit to another page the Main Menu Items disapper. My observation is that this is the problem of ViewBack because i am creating menu from the ViewwBack and when another page or View is displayed the ViewBack scope is finished therefore in another View the ViewBack is null so it clears the Menu Items. This isjust my observation. Please help me in this situation how can i solve this problem. Following is my Code of _Layout.schtml:
 
@if (ViewBag.LoginSession != null)
{
var MenuMaster = ViewBag.LoginSession as List;
var groupByMenu = MenuMaster.GroupBy(x => x.MainMenuName);
@foreach (var items in groupByMenu)
{
@items.Key
@foreach (var SubMenuList in items)
{
@SubMenuList.SubMenuName
}
}
}
@if (ViewBag.LoginSession != null)
{
@Html.ActionLink("Log Out", "LogOut", "Login")
}
else
{
@Html.ActionLink("Login", "Login", "Login")
}
 
Please give me your valuable solution in this context. Thanks

Answers (3)