【问题标题】:Actionlink to another view (MVC)Actionlink 到另一个视图 (MVC)
【发布时间】:2017-07-20 11:33:06
【问题描述】:

我正在处理我的 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 viewProductList.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);
        }

    }
}

问题:我如何在主页上按一个类别,这将带我进入显示该类别产品的页面,我该怎么做?

提前致谢 :)

【问题讨论】:

  • 这段代码你遇到了什么问题?
  • 您想在同一页面或新的不同页面中显示特定类别的产品?
  • 在新的不同页面@BasantaMatia
  • @ChetanRanpariya ,没有问题,我在主页上有类别及其工作,但只需要知道如何将每个类别的产品链接到另一个页面,所以我可以在另一个页面上看到此类产品
  • 那你为什么要创建一个局部视图ProductList.cshtml。它应该是一个主视图,您可以在其中将产品显示到特定类别。 您在哪里获取某个类别的产品的操作方法??

标签: c# html asp.net-mvc controller actionresult


【解决方案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>

干杯!!

【讨论】:

    【解决方案2】:

    您的操作链接错误。您需要使用参数名称调用您的 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视图中绑定数据。

    希望对你有帮助:)

    【讨论】:

    • 我必须为我的产品列表部分视图添加此操作方法吗? @BasantaMatia
    • 我得到: 说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.ArgumentException:参数字典包含“MVCOnlineShop.Controllers”中方法“System.Web.Mvc.ActionResult ProductList(Int32)”的不可为空类型“System.Int32”的参数“id”的空条目。家庭控制器”。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
    • 您需要在您的 CategoryLayout.cshtml 部分视图中编写该操作方法,即您发布的相关代码。您是否有类别的 id 属性?您需要在此处传递 @Html.ActionLink(Category.CategoryName, "MyProductDetails","YourControllerName", new { id = Category.CategoryId }) 代替 CategoryId 只需写下即可。
    • 如何在局部视图中编写动作结果?!
    • 这是一个基本的东西。如果您的 Categories Id 字段可以为空,那么您需要在您的操作方法中添加一个 ?,例如 public ActionResult MyProductDetails(int? id)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多