【问题标题】:Show partial page within a view page在视图页面中显示部分页面
【发布时间】:2016-03-09 19:34:15
【问题描述】:

我有一个推荐视图模型

推荐人视图模型

public class RecommenderViewModel
{
    public string ProName { get; set; }
    public int? OdId { get; set; }
    public int? OrdId { get; set; }
    public string CusName { get; set; }
    public string CatName { get; set; }
    public int catId { get; set; }
    public string SubCatName { get; set; }
    public int subcatId { get; set; }
    public string SubSubCatName { get; set; }
    public int subsubcatId { get; set; }
}

控制器

    public ActionResult Index()
    {
        private Shopping db = new Shopping();
        string userID = User.Identity.GetUserId();

        List<RecommenderViewModel> model = new List<RecommenderViewModel>();
        var innerJoinQuery = (from pro in db.Products
                              join sup in db.OrderDetails on pro.ProductId equals sup.ProductID
                              join ord in db.Orders on sup.OrderId equals ord.OrderId
                              join cus in db.Users on ord.UserId equals cus.Id where cus.Id == userID
                              join cat in db.Categories on pro.CategoryId equals cat.CategoryId
                              join subcat in db.SubCategories on pro.SubCategoryId equals subcat.SubCatId
                              join subsubcat in db.SubSubCategories on pro.SubSubCategoryId equals subsubcat.SubSubCatId

                              select new
                              {   
                                  proName = pro.Name,
                                  odId = sup.OrderDetailId,
                                  ordId = ord.OrderId,
                                  cusName = cus.FirstName,
                                  catName = cat.Name,
                                  catId = cat.CategoryId,
                                  subcatName = subcat.SubCatName,
                                  subcatId = subcat.SubCatId,
                                  subsubcatName = subsubcat.SubSubCatName,
                                  subsubcatId = subsubcat.SubSubCatId
                              }).ToList();//convert to List


        foreach (var item in innerJoinQuery)
        {
            model.Add(new RecommenderViewModel()
            {
                ProName = item.proName,
                OdId = item.odId,
                OrdId = item.ordId,
                CusName = item.cusName,
                CatName = item.catName,
                catId = item.catId,
                SubCatName = item.subcatName,
                subcatId = item.subcatId,
                SubSubCatName = item.subsubcatName,
                subsubcatId = item.subsubcatId
                //recProName = q 
            });
        }
        return View(model);
    }

来自上面的索引操作和来自 RecommenderController 的查询。我得到了我想要的结果。 Picture of result 在视图中。

现在我有另一个 ProductDetails 视图页面,它显示产品的详细信息,如产品名称、价格、数量、描述,我想创建 Index() 的部分视图并将其显示在产品详细信息视图页面中,这是一个强类型视图并使用 @model Project.Models.Product

Product.cs

public partial class Product {
   public int ProductId {get; set;}
   public int ProductName {get; set;}
   public int ProductDescription {get; set;}
   public int ProductPrice {get; set;}
   public int ProductIdQuantity {get; set;}
}

【问题讨论】:

  • 如果我没听错的话,如果您想显示产品详细信息以及类似亚马逊或其他零售网站的类似类别的推荐产品?
  • @user1672994 是的。但在我的代码中,我所做的只是获取登录用户的先前购买记录并匹配产品 id 并获取该购买产品的类别并向他展示更多类别中的产品
  • @user1672994 稍后我也会这样做,我猜会使用 cookie。我会尝试向他推荐他正在观看的产品,我想我将不得不为此目的存储他的会话或 cookie。
  • 您可以将查看的项目放在会话或数据库中(这将是您的选择)。现在回答你的问题,你可以为接受 int? 的索引创建一个局部视图。产品 ID 以显示所有产品与选定产品。我会在几分钟内发布答案。

标签: c# asp.net asp.net-mvc razor


【解决方案1】:

为它创建一个 ChildAction 和 Partial 视图。

[ChildActionOnly]
public ActionResult RetrieveProductDetails(int? productId)
{
}

现在,您的 Index 操作将返回将呈现视图的索引视图

@Html.RenderAction("RetrieveProductDetails", "Home");

您可以在 ProductDetails 页面中使用相同的逻辑,唯一的区别是您将传递产品 ID

@Html.RenderAction("RetrieveProductDetails", "Home", new { @productId = Model.ProductId });

【讨论】:

  • 我不认为这是一个解决方案。我在 Recommender 控制器中有 Index Action,在 Home Controller 中有 ProductDetails Action。
  • RenderAction 的第二个参数是控制器名称。所以改成@Html.RenderAction("RetrieveProductDetails", "Recommender")
  • 谢谢伙计。我让它工作了。它给出了一个错误“无法将 void 隐式转换为对象”,然后我这样写 @{Html.RenderAction("RetrieveProductDetails", "Recommender");} 并且它起作用了。
  • 关于按用户浏览历史或他最近查看的产品显示推荐产品的任何想法?还是我应该问另一个问题?
  • 您应该以这样一种方式构建您的系统,即它可以审计用户查看的产品的持久存储(可以是数据库)。因此,在显示时您可以查询以获取最近查看或浏览的历史记录。
猜你喜欢
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2018-11-12
  • 2014-02-21
  • 2013-06-04
  • 2014-05-16
相关资源
最近更新 更多