环通过多层次动态菜单在Asp.Net MVC [英] Loop Through Multi-Level Dynamic Menus in Asp.Net MVC
问题描述
我公司通过多层次的动态菜单试图循环。我已经成功地做到这一点手动即每次如果要显示的子菜单
它的父,我必须手动循环。 我想知道最好的方式还是通过这些动态菜单循环多级的替代
以下是我迄今所做的;
i am trying to loop through multilevel dynamic menus. I have been succeeded to do this manually i.e everytime if want to display the child Menus
of it's parent, I have to loop manually. I would like to know the best way or an alternative of looping multi-level through these dynamic menus
Here is what i have done so far;
@{ var menusList = ViewBag.Menus as IEnumerable<ParentMenuViewModel>; }
@foreach (var parentMenu in menusList.Where(p => p.ParentId == 0))
{
<ul>
<li>
<h1>@parentMenu.Name</h1>
@if (menusList.Count(p => p.ParentId == parentMenu.MenuId) > 0)
{
<ul>
@foreach (var childMenu in menusList.Where(p => p.ParentId == parentMenu.MenuId))
{
<h2>@childMenu.Name</h2>
if (menusList.Count(p => p.ParentId == childMenu.MenuId) > 0)
{
foreach (var subChild in menusList.Where(p => p.ParentId == childMenu.MenuId))
{
<h3>@subChild.Name</h3>
}
}
}
</ul>
}
</li>
</ul>
}
更新:输出看起来像这样;
HOME
SUB MENU1
SUB SUB MENU1
SUB SUB MENU2
不过,我已经在我的数据库是这样的;
However, i have in my database something like this;
HOME
SUB MENU1
SUB SUB MENU1
SUB SUB MENU2
Sub SUB SUB MENU1
Sub SUB SUB MENU2
下面是我的模型;
Here is my model;
推荐答案
您可以使用一个partialview,然后做一个递归循环。为了做到这一点你首先需要改变你的模型位:
You could use a partialview and then do a recursive loop. In order to do that you'd first need to change your Model a bit:
视图模型
// The ViewModel is now a hirearchical model, where each item has a list of children.
public class MenuViewModel
{
int MenuId {get; set;}
string Name {get; set;}
//other properties
** snip **
List<MenuViewModel> Children {get; set;}
}
控制器
变换模式进入层次视图模型:
Transform model into the hierarchical ViewModel:
public ActionResult Menus(){
List<Menu> menusource; // get your menus here
ViewBag.Menus = CreateVM(0, menusource); // transform it into the ViewModel
return View();
}
public IEnumerable<MenuViewModel> CreateVM(int parentid, List<Menu> source)
{
return from men in source
where men.ParentId = parentid
select new MenuViewModel(){
MenuId = men.MenuId,
Name = men.Name
// other properties
Children = CreateVM(men.MenuId, source)
};
}
查看
@{
var menusList = ViewBag.Menus as IEnumerable<MenuViewModel>;
Html.RenderPartial("MenuPartial", menuslist);
}
MenuPartial
@model IEnumerable<MenuViewModel>
@foreach (var menuitem in model)
{
<ul>
<li>
<h1>@menuitem.Name</h1>
@{
Html.RenderPartial("MenuPartial", menuitem.Children);
}
</li>
</ul>
}
您将在这里与问候你原来的code会丢失的唯一的事情是,你没有不同HX-的标签,但你可以通过创建另一个视图模型,并通过它,你的水平找到办法解决目前在
The only thing you will be missing here with regards to your original code is that you don't have different Hx-tags, but you could find a way around that by creating another viewmodel and passing it the level you are currently at.
请注意:我输入了这一切code在SO-编辑器,因此可能会有一些小的语法错误
Note: I typed up all this code in the SO-editor, so there may be some small syntax errors.
这篇关于环通过多层次动态菜单在Asp.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!