如何通过创建子类动态子菜单 [英] How to creating a Dynamic sub-menus by sub-categories
问题描述
我设置一个菜单子菜单
显示子类别
,数据库我创建了一个列 isSelected
与布尔数据类型。如果只子类别
是 isSelected ==主页上真正
,它会显示。我不知道如何设置子类别
与 isSelected ==关于
在真正
显示菜单头
。
I'm setting a Menu with Sub-menu
display Sub-categories
, in database I created a column isSelected
with Boolean data type. If only Sub-categories
is isSelected == true
, it will be display on main page. I'm wondering how to setting Sub-categories
with isSelected == true
display on Menu
on the header
.
IQueryable<ProductSubcategory> list = null;
if (Id == null)
{
list = BikesDB.ProductSubcategories;
}
else
{
int id = Id.Value;
list = BikesDB.ProductSubcategories.Where
(m => m.ProductSubcategoryID == id && m.NameofBike == Name);
}
var bikes = list.Where(m => m.isSelected == true)
.AsEnumerable().Select
(p => new Bike { Id = p.ProductSubcategoryID, Name = p.NameofBike });
var viewModel = new CategoriesIndexViewModel
{
NumberOfModel = bikes.Count(),
NameofBike = bikes.Select(b=>b.Name).ToList(),
Bikes = bikes.ToList()
};
return this.View(viewModel);
现在我只是硬code在 HTML
三个子菜单:
Now I just hard-code three sub-menus in HTML
:
<li>
<a href="@Url.Content("~/Bike/")">Home</a>
<ul>
<li>
<a href="@Url.Content("~/Bike/Categories/1?name=Mountain Bikes&class=image")">Mountain Bikes</a>
</li>
<li>
<a href="@Url.Content("~/Bike/Categories/2?name=Road Bikes&class=image")">Road Bikes</a>
</li>
<li>
<a href="@Url.Content("~/Bike/Categories/3?name=Touring Bikes&class=image")">Touring Bikes</a>
</li>
</ul>
如何解决它动态地根据子类别
主页上显示?
How to fix it dynamically based on Sub-categories
display on main page ???
推荐答案
检查是否这会有所帮助。
Check if this helps.
public abstract class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
IEnumerable<MVC3Stack.Models.MenuItem> menus = BuildMenu();
ViewBag.Menus = menus;
//base.OnActionExecuting(filterContext);
}
private IEnumerable<MVC3Stack.Models.MenuItem> BuildMenu()
{
IEnumerable<MVC3Stack.Models.MenuItem> menus = new List<MVC3Stack.Models.MenuItem>
{
new MVC3Stack.Models.MenuItem{Id = 1, Level = 0, ParentId = 0, Text = "Main", Url = Url.Action("Index", "Home"), IsSelected=true, HasChildren=true },
new MVC3Stack.Models.MenuItem { Id = 2, Level = 1, ParentId = 1, Text = "Main-SubMenu1", Url = Url.Action("Index", "Home"), IsSelected=false, HasChildren=false },
new MVC3Stack.Models.MenuItem { Id = 3, Level = 1, ParentId = 1, Text = "Main-SubMenu2", Url = Url.Action("Index", "Home"), IsSelected=true , HasChildren=false},
new MVC3Stack.Models.MenuItem { Id = 4, Level = 0, ParentId = 0, Text = "Second Menu", Url = Url.Action("Index", "Home") ,IsSelected=true, HasChildren=true},
new MVC3Stack.Models.MenuItem { Id = 5, Level = 1, ParentId = 4, Text = "Second Menu-SubMenu1", Url = Url.Action("Index", "Home"),IsSelected=true, HasChildren=false }
};
return menus;
}
}
下面是_layout.cshtml
Here is the _layout.cshtml
@{
var topLevel = ((IEnumerable<MVC3Stack.Models.MenuItem>)ViewBag.Menus).Where(x => x.Level == 0);
}
<ul id="menu">
@foreach (var item in topLevel)
{
if (item.IsSelected)
{
<li>
<a href="@Url.Action("Index", "Home")">@item.Text</a>
@if (item.HasChildren)
{
var level1 = ((IEnumerable<MVC3Stack.Models.MenuItem>)ViewBag.Menus).Where(x => x.Level == 1 && x.ParentId == item.Id);
<ul>
@foreach (var item1 in level1)
{
if (item1.IsSelected)
{
<li>
<a href="@Url.Action("Home", "Index")">@item1.Text</a>
</li>
}
}
</ul>
}
</li>
}
}
</ul>
这可以给你如何implemente一些这方面的指针。
注:级别数将固定使用此解决方案
This can give you some pointers on how to implemente this. Note: Number of levels will be fixed with this solution.
这篇关于如何通过创建子类动态子菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!