2017-07-15 39 views
0

我对我的MVCOnlineShop工作,这是我迄今取得: Image 我在View之前写了一个代码,这样我就可以将产品链接他们的类别和它的工作,所有类别和产品数据库在SQL server。这是代码:获取数据,以引导MVC html的悬停下拉列表

@model MVCOnlineShop.Models.Category 

@{ 
    ViewBag.Title = "Browse"; 
} 
<h2>Browsing Category: @Model.CategoryName</h2> 
<ul> 
    @foreach (var Product in Model.Products) 
    { 
     <li> 
      @Html.ActionLink(Product.ProductName, 
"Details", new { id = Product.CategoryID }) 
     </li> 
    } 
</ul> 

问题:现在,我怎么用这个代码显示在他们的类别产品中的bootstrap下拉列表?所以我想点击悬停在游戏例如,我想得到一个下拉列表,有游戏1,游戏2,游戏3.谢谢!这就是我想在我的_Layout.cshtml

@using MVCOnlineShop.Models; 

@{ 
    // stores the Session content in a var 
    var Categories = Session["Categories"] as List<Category>; 
} 

@*Checks if the Session variable is correct*@ 
@if (Categories != null) 
{ 
    <ul class="nav navbar-nav"> 
     @*For each category in the Session var, display the link*@ 

      @foreach (var Category in Categories) 
      { 
       <div class="dropdown"> 
        <button class="dropbtn">@Html.ActionLink(Category.CategoryName, "Browse", new { Category = Category.CategoryName })</button> 
       <div class="dropdown-content"> 
    <a href="#">Link 1</a> 
    <a href="#">Link 2</a> 
    <a href="#">Link 3</a> 
    </div> 
</div> 

      } 

</ul> 
} 

,这是从SQL类类,Category.cs

namespace MVCOnlineShop.Models 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class Category 
    { 
     public Category() 
     { 
      this.Products = new HashSet<Product>(); 
     } 

     public int CategoryID { get; set; } 
     public string CategoryName { get; set; } 
     public string Description { get; set; } 

     public virtual ICollection<Product> Products { get; set; } 
    } 
} 
+0

希望这可以帮助你的兄弟:) - > 【答案】(https://stackoverflow.com/questions/43087175/mvc -dynamic-menu-populating-plain-text/43141577#43141577) – ARr0w

回答

0

我做了一个CategoryLayout.cshtmlPartial View,它的工作,现在我可以看到类别与下他们的产品从SQL所有数据里斯,这是代码:

@model IEnumerable<MVCOnlineShop.Models.Category> 
@{ 
    ViewBag.Title = "CategoryLayout"; 
} 

@foreach (var Category in Model) 
{ 
    <li> 
     <div class="dropdown"> 
      <button class="dropbtn"> 
       @Html.ActionLink(Category.CategoryName, 
"ProductList", new { Category = Category.CategoryID }, new { @style = "color:#1ABC9C;text-decoration:none;" }) 
     </button> 

     <div class="dropdown-content"> 
      @foreach (var Product in Category.Products) 
      { 
       @Html.ActionLink(Product.ProductName, 
    "Details", new { id = Product.CategoryID }, new { style = "text-decoration:none;" }) 
      } 
     </div> 
    </div> 
</li> 
} 
1

你的foreach循环应该是:

@foreach(var category in Categories) 
    { 
     @Html.LabelFor(category.CategoryName) 
     @foreach(var product in category.Products) 
     { 
     <div class="dropdown"> 
      <div class="dropdown-content"> 
      <button class="dropbtn">@Html.ActionLink(product.ProductName, "ActionName", "ControllerName", new { id = Product.CategoryID, title = Product.ProductName }, null)</button> 
      </div> 
     </div> 
     } 
    } 
+0

你在类别中有价值吗? –

+0

添加类别模型 –

+0

我要求用view @foreach(var类别中的类别)检查断点,并且得到值或不是 –