您好我正在尝试在布局页面中动态加载菜单。
例如
1)AccountsMasters
1.1)AccountsCategory
1.2)Accounts
在这里我加载了帐户类别的子菜单,动态帐户,但我无法动态加载帐户主菜单。我尝试加载主菜单,就像子菜单一样,但加载2次就是 例如
1)AccountsMasters
1.1)AccountsCategory
1)AccountsMasters
1.2)Accounts
它加载主菜单两次
我的表
UID MainMenuName SubMenuName MAINMENIID
1 ACCOUNTSMASTER账户1
2 ACCOUNTMASTER ACCOUNTSCATEGORY 1
3 PRODUCTMASTER PRODUCTS 2
4 PRODUCTMASTER PRODUCTCATEGORY 2
我的模型SubMenuViewModel
public class SubMenuviewmodel
{
public string SubMenu { get; set; }
public string ActionName { get; set; }
public string MainMenuName { get; set; }
public string ControllerName { get; set; }
}
菜单型号
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; }
}
我的控制器
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);
}
我的观看代码
@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>
此代码给出了上面提到的结果。它给出了错误的输出。任何人告诉我我做了什么错。我尽力解释这个问题。任何一个人都能理解我的问题并帮助我解决这个问题。
提前谢谢..
答案 0 :(得分:1)
如果你尝试这样的话怎么样
@model DynamicMenuLoading.Models.Menu
@{
Layout = null;
}
<ul>
<li>
@{
string name = "";
foreach (var item in Model.SubMenuItems)
{
if (item.MainMenuName != name)
{
name = item.MainMenuName;
<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>