我试图每个产品下创建用户评论,我用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]'.
任何帮助将是伟大的。
您需要了解'RenderAction'和'RenderPartial'之间的区别。首先你要调用动作,但第二,你直接调用局部视图。所以你不能在'RenderPartial'中传递'productId',而是需要传递'List'。同样在'RenderPartial'中,您需要给出部分视图名称,而不是操作名称。 –
ramiramilu
@ramiramilu,太感谢你了,我创建了一个由PartialView点击右键添加视图,检查创建局部视图,现在,它的工作原理,确定这是快速Html.RenderPartial或Html.RenderAction? – stom
这取决于,如果你正在使用'RenderAction',你一次又一次地调用相同的LINQ查询这是昂贵的,因为DB命中。但是,如果你想一次获得所有项目,然后使用'RenderPartial',那么你可以避免大部分数据库命中。 – ramiramilu