在视图页面中显示部分页面

本文关键字:显示部 分页 视图 | 更新日期: 2023-09-27 18:25:38

我有一个推荐视图模型

推荐者视图模型

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

从上面的索引操作和来自RecommendarController的查询。我得到了我想要的结果。视图中结果的图片。

现在,我有另一个ProductDetails的视图页面,它显示产品的详细信息,如ProductName、价格、数量、描述,我想创建Index()的部分视图,并将其显示在产品详细信息视图页面中,该页面是一个强类型视图,使用@model Project.Models.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;}
}

在视图页面中显示部分页面

为其创建ChildAction和Partial视图。

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

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

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

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

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