我正在处理 MVCOnlineShop项目 ,我在主页上通过创建partial view
CategoryLayout.cshtml
显示了类别:
@model IEnumerable<MVCOnlineShop.Models.Category>
@{
ViewBag.Title = "CategoryLayout";
}
<ul class="nav navbar-nav">
@foreach (var Category in Model)
{
<li>
@Html.ActionLink(Category.CategoryName,
"ProductList", new { Category = Category.CategoryName })
</li>
</ul>
并在_Layout.cshtml
:
@Html.Partial("CategoryLayout")
现在我想按主页上的任何类别,这将带我到这类别的产品,我创建了这个partial view
ProductList.cshtml
:
@model MVCOnlineShop.Models.Category
@{
ViewBag.Title = "ProductList";
}
<ul>
@foreach (var Product in Model.Products)
{
<li>
@Html.ActionLink(Product.ProductName,
"Details", new { id = Product.CategoryID })
</li>
}
</ul>
这是我的HomeController
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOnlineShop.Models;
namespace MVCOnlineShop.Controllers
{
public class HomeController : Controller
{
OnlineStoreEntities storeDB = new OnlineStoreEntities();
//
// GET: /Home/
public ActionResult Index()
{
var Categories = storeDB.Categories.ToList();
return View(Categories);
}
//
// GET: /Home/Browse
public ActionResult Browse(string Category)
{
// Retrieve Category and its Associated Products from database
var CategoryModel = storeDB.Categories.Include("Products")
.Single(g => g.CategoryName == Category);
return View(CategoryModel);
}
//
// GET: /Home/Details
public ActionResult Details(int id)
{
var Product = storeDB.Products.Find(id);
return View(Product);
}
//
// GET: /Home/Browse?Category=Games
public ActionResult CategoryLayout()
{
var Categories = storeDB.Categories.ToList();
return PartialView("CategoryLayout", Categories);
}
}
}
问题:我如何按主页上的某个类别,这将带我到显示此类别产品的页面,我该怎么做?
提前致谢:)
答案 0 :(得分:1)
我们走了:
首先,您的操作链接应为:
<li>
@Html.ActionLink(Category.CategoryName,
"ProductList", new { CategoryId = Category.CategoryName })
</li>
然后您必须在控制器中添加ProductList
操作,例如:
public ActionResult ProductList(int? CategoryId)
{
Category objCategory = new Category();
objCategory.Products = storeDB.Products.where(m => m.CategoryId == CategoryId).ToList();
return PartialView("ProductList", objCategory);
}
您的产品部分视图应为:
@model MVCOnlineShop.Models.Category
@{
ViewBag.Title = "ProductList";
}
<ul>
@foreach (var Product in Model.Products)
{
<li>
@Html.ActionLink(Product.ProductName,
"Details", new { id = Product.CategoryID })
</li>
}
</ul>
干杯!!
答案 1 :(得分:0)
您的操作链接错误。您需要使用参数名称调用Action方法,例如
<li>
@Html.ActionLink(Category.CategoryName,
"MyProductDetails","YourControllerName",
new { id = Category.CategoryId })
</li>
注意:您可以在上面的代码中更改操作方法名称。并且insint为categoryName我正在传递id。但是你也可以传递categoryName。没问题。
然后在Action Method
,
public ActionResult MyProductDetails(int? id) // If your id is not nullable then you can remove ? mark
{
List<ProductViewModel> Products = new List<ProductViewModel>();
if(id!=null)
{
Products = storeDB.Products.where(m=>m.categoryId == id).ToList(); // You can fetch the product as you like
return View(Products);
}
else
return View(Products); // It will return null
}
然后,您可以在MyProductDetails
视图中绑定数据。
希望有所帮助:)