0

我试图每个产品下创建用户评论,我用Html.RenderAction如何使用Html.RenderPartial与视图模型

Html.RenderAction("ProductReviewTest", new { id = productids }); 

它工作正常,但它需要9.4s与评论加载产品页面,所以试图Html.RenderPartial但给人错误

我的产品的看法:

@model MVCProduct.Models.Product 

<!--here displaying products--> 

<!--displaying reviews in same view--> 

<div class="display-field"> 
<p> Reviews for @Html.DisplayFor(model => model.ProductTitle) </p> 
@{ 

int productid = Model.ProductID; 

Html.RenderPartial("ProductReviewTest", new { id = productid }); 

} 

</div> 

我的评论观点米奥德尔:

public class ProductViewModel 
{ 
    public int ReviewId { get; set; } 
    public int? ProductID { get; set; } 
    public string ReviewTitle { get; set; } 
    public string ReviewMessage { get; set; } 
    public int? Rating { get; set; } 
    public string CustomerName { get; set; } 
    public string ReviewStatus { get; set; } 

} 

我的ViewResult:

public PartialViewResult ProductReviewTest(int id) 
    { 

    List<ProductViewModel> productviewmodel = (from a in dbo.ProductReviews 
    where a.ProductID ==id 
    select new ProductViewModel 
     { 
      ReviewId=a.ReviewId, 
      ProductID=a.ProductID, 
      ReviewTitle =a.ReviewTitle, 
      ReviewMessage =a.ReviewMessage, 
      Rating =a.Rating, 
      CustomerName =a.CustomerName, 
      ReviewStatus=a.ReviewStatus 
     }).ToList(); 



     return PartialView(productviewmodel); 
    } 

我的检讨观点:

@model IEnumerable<MVCProduct.Models.ProductViewModel> 

<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.ReviewId) 
     </th> 
....... 

</table> 

错误:

The model item passed into the dictionary is of type '<>f__AnonymousType5 1[System.Int32]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[Review.Models.ProductViewModel]'.

任何帮助将是伟大的。

+4

您需要了解'RenderAction'和'RenderPartial'之间的区别。首先你要调用动作,但第二,你直接调用局部视图。所以你不能在'RenderPartial'中传递'productId',而是需要传递'List '。同样在'RenderPartial'中,您需要给出部分视图名称,而不是操作名称。 – ramiramilu

+0

@ramiramilu,太感谢你了,我创建了一个由PartialView点击右键添加视图,检查创建局部视图,现在,它的工作原理,确定这是快速Html.RenderPartial或Html.RenderAction? – stom

+0

这取决于,如果你正在使用'RenderAction',你一次又一次地调用相同的LINQ查询这是昂贵的,因为DB命中。但是,如果你想一次获得所有项目,然后使用'RenderPartial',那么你可以避免大部分数据库命中。 – ramiramilu

回答

2

RenderActionRenderPartial之间的差。首先你要调用动作,但第二,你直接调用局部视图。

所以你不能通过RenderPartialproductId,而是你需要通过List<ProductViewModel>。同样在RenderPartial中,您需要给出部分视图名称,而不是操作名称。

+0

嗨,多一个查询,是否使用具有'返回PartialView'和'ActionResult'的'PartialViewResult'有任何区别?在使用'Html.RenderPartial'时使用'return View'? – stom

+0

通常'ViewResult'和'PartialViewResult'之间没有区别,都呈现响应流。 – ramiramilu

0

您正在返回一个ProductViewModel列表来查看。 而是使用

var productviewmodel = (from a in dbo.ProductReviews 
where a.ProductID ==id 
select new ProductViewModel 
    { 
     ReviewId=a.ReviewId, 
     ProductID=a.ProductID, 
     ReviewTitle =a.ReviewTitle, 
     ReviewMessage =a.ReviewMessage, 
     Rating =a.Rating, 
     CustomerName =a.CustomerName, 
     ReviewStatus=a.ReviewStatus 
    }).FirstOrDefault(); 

回报PartialView(productviewmodel);

1

的ViewResult:

public PartialViewResult ProductReviewTest() 
{ 
    return PartialView(); 
} 

产品视图:

@model MVCProduct.Models.Product 

<!--here displaying products--> 

<!--displaying reviews in same view--> 

<div class="display-field"> 
<p> Reviews for @Html.DisplayFor(model => model.ProductTitle) </p> 
@{ 

int productid = Model.ProductID; 

Html.RenderPartial("ProductReviewTest", Model.ProductReviews }); 

} 

</div> 
+0

谢谢@joaoeduardorf,但我想你忘了提及创建部分视图 – stom