Sneha K

Sneha K

  • 1.2k
  • 527
  • 196.8k

Dynamic Menu is not loading properly in mvc?

Mar 15 2017 1:45 PM

Hi i am trying to load menu dynamically in Layout page.

Eg

 
1)AccountsMasters
1.1)AccountsCategory
1.2)Accounts
 
Here I load the submenus that is accounts category,accounts dynamically but i can't able to load Accounts Master main menu dynamically.I tried to load the main menu same as like submenu but its loading 2 times that is eg
 
 
1)AccountsMasters
1.1)AccountsCategory
1)AccountsMasters
1.2)Accounts
 

its load the main menu two times

My Table

UID MainMenuName SubMenuName MAINMENIID

1 ACCOUNTSMASTER ACCOUNTS 1

2 ACCOUNTMASTER ACCOUNTSCATEGORY 1

3 PRODUCTMASTER PRODUCTS 2

4 PRODUCTMASTER PRODUCTCATEGORY 2

My model SubMenuViewModel
 
 
public class SubMenuviewmodel
{
public string SubMenu { get; set; }
public string ActionName { get; set; }
public string MainMenuName { get; set; }
public string ControllerName { get; set; }
}
Menu Model
 
 
public class Menu
{
public Menu()
{
MenuItems = new List<MenuItem>();
SubMenuItems = new List<SubMenuviewmodel>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<MenuItem> MenuItems { get; set; }
public List<SubMenuviewmodel> SubMenuItems { get; set; }
}
My Controller
 
 
public ActionResult SubMenu()
{
var menu = new Menu();
var submenu = db.SubAplicationNames.Select(e => new
{
submenuuid = e.SubAplicationNameID,
submenuname = e.SubMenuName,
submenuparentidd = e.SubAplicationNameID,
mainmenuname=e.ApplicationName
}).ToList();
var submenulist = new List<SubMenuviewmodel>();
for (var item = 0; item < submenu.Count();item++ )
{
var submenuitemslist = new SubMenuviewmodel { SubMenu = submenu[item].submenuname.ToString(),MainMenuName=submenu[item].mainmenuname
,ControllerName="",ActionName="" };
submenulist.Add(submenuitemslist);
}
menu.SubMenuItems = submenulist;
return View(menu);
}
 
 
My View Code
 
@model DynamicMenuLoading.Models.Menu
@{
Layout = null;
}
<ul>
<li>
@foreach(var item in Model.SubMenuItems)
{
<a href="#">
<span>@item.MainMenuName</span>
</a>
<ul>
<li><a href="@Url.Action(item.ActionName , item.ControllerName )"><i class="fa fa-circle-o"></i>@item.SubMenu</a></li>
</ul>
}
</li>
</ul>

This code gives the result which is mention above.it giving wrong output. Any one tell me what mistake i did. I tried my level best to explain the issue.Any one understand my issue and help me to resolve this problem.

Advance Thanks..

 

Answers (3)