我正在做我的MVCOnlineShop
,我在navbar
的主页上制作了一个dropdownlist
,它的类别是dropdown
按钮,产品是dropdowncontent
,我想把这个放在ProductList
View
上。
这是我的CategoryLayout.cshtml
(PartialView
):
@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>
}
这是我的PartialView
ProductList.cshtml
@model MVCOnlineShop.Models.Category
@{
ViewBag.Title = "ProductList";
}
<script src="~/Scripts/Productjs/bootstrap.min.js"></script>
<script src="~/Scripts/Productjs/jquery.js"></script>
<link href="~/Content/Productcss/bootstrap.min.css" rel="stylesheet">
<link href="~/Content/Productcss/2-col-portfolio.css" rel="stylesheet">
<div class="container">
<!-- Page Header -->
<div class="row">
@foreach (var Product in Model.Products)
{
<div class="col-md-6 portfolio-item">
<a href="#">
<img class="img-responsive" src="@Product.ImageID" alt="">
</a>
<h3>
<a href="#">@Html.ActionLink(Product.ProductName, "Details", new { id = Product.CategoryID })</a>
</h3>
</div>
}
</div>
</div>
这就是我如何在CategoryLayout.cshtml
中呈现_Layout.cshtml
,以在homepage
上显示dropdownlist
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
@Html.Partial("CategoryLayout")
</ul>
</div>
</div>
</div>
问题:如何在ProductList
上显示这个navbar
?
,提前谢谢!
发布于 2017-08-07 15:01:29
创建一个儿童行动:
[ChildActionOnly]
public ActionResult Navbar()
{
var categories = // get categories;
return PartialView("_Navbar", categories);
}
然后,创建一个部分视图来呈现导航条(_Navbar.cshtml
):
@model IEnumerable<MVCOnlineShop.Models.Category>
@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>
}
然后,在您希望导航条出现的地方调用以下命令:
@Html.Action("Navbar", "Foo")
其中"Foo"
是添加子操作的控制器的名称。然后,视图可以使用它需要的任何模型,并且您仍然可以使用它自己的模型来呈现您的导航条。
发布于 2017-08-07 15:03:18
如果我理解的话,我认为您正在寻找的是一个母版页。
MSDN说:
ASP.NET母版页允许您为应用程序中的页创建一致的布局。单个母版页定义了应用程序中所有页面(或一组页面)所需的外观和感觉以及标准行为。
注意:标准行为,您想要的所有页面,...in,您的应用程序。
所以你要做的是重新考虑你的方法。必须出现在每个页上的每个元素都将在母版页视图中定义。
请阅读这个关于创建母版页和子页的MSDN文章
简单地说,您的主页应该是这样的:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="you_file_name.cs" Inherits="Site" %>
<html>
<head>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
<!-- some other elements like scripts, css, etc...-->
</head>
<body>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">@Html.Partial("CategoryLayout")</ul>
</div>
<!-- Here add the code for rendering any other view at will ; for instance ProductList -->
</body>
</html>
尽管如此:您希望在母版页中呈现的所有其他视图都必须相应地创建(用母版页创建视图内容.并在视图创建对话框上选择适当的母版页)
https://stackoverflow.com/questions/45547135
复制相似问题