【发布时间】:2017-12-20 08:38:39
【问题描述】:
我正在开发我的 MVCOnlineShop ,这是我目前所做的:
我之前在View 中编写了一个代码,因此我可以将产品链接到它们的类别并且它有效,所有类别和产品数据库都在SQL server 中。这是代码:
@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; }
}
}
【问题讨论】:
-
希望这对你有所帮助 :) --> Answer
标签: c# jquery html twitter-bootstrap asp.net-mvc-4