动作链接到另一个视图 (MVC)

Actionlink to another view (MVC)

我正在开发我的 MVCOnlineShop 项目,我通过创建 partial view [=15] 在主页上显示类别=]:

@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);
        }

    }
}

问题:我怎样才能在主页上点击一个类别,这将带我到一个显示该类别产品的页面,我该怎么做?

提前致谢:)

您的操作 link 是错误的。您需要使用参数名称调用您的 Action 方法,例如,

 <li>
   @Html.ActionLink(Category.CategoryName,
      "MyProductDetails","YourControllerName", 
      new { id = Category.CategoryId })
 </li>

注意:您可以更改上面代码中的Action方法名称。我传递的是 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 视图中绑定数据。

希望对您有所帮助:)

我们开始了:

首先你的操作link应该是:

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

干杯!!