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

时间:2015-12-04 19:01:48

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

我有一个推荐人视图模型

推荐人视图模型

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

来自上面的Index Action和来自RecommenderController的查询。我得到了我想要的结果。视图中Picture of result

现在我有另一个ProductDetails的View页面,它显示ProductName,Price,Quantity,Description等产品的详细信息,我想创建一个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;}
}

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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

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