如何按子类创建动态子菜单

时间:2014-03-19 06:43:08

标签: html asp.net-mvc-4 c#-4.0

我设置了一个带Sub-menu显示Sub-categories的菜单,在数据库中我创建了一个带有布尔数据类型的列isSelected。如果仅Sub-categoriesisSelected == true,则会显示在主页上。我想知道如何在Sub-categories上的isSelected == trueMenu显示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);

现在我只需在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>

如何基于主页上的Sub-categories显示动态修复它?

1 个答案:

答案 0 :(得分:0)

检查这是否有帮助。

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

@{
  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>

这可以为您提供有关如何实现此功能的一些指示。 注意:此解决方案将修复级别数。