如何将 Html.RenderPartial 与 ViewModel 一起使用
how to use Html.RenderPartial with ViewModel
我正在尝试在每个产品下创建用户评论,我使用了 Html.RenderAction
Html.RenderAction("ProductReviewTest", new { id = productids });
它工作正常,但需要 9.4 秒才能加载带有评论的产品页面,因此尝试 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; }
}
我的查看结果:
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__AnonymousType51[System.Int32]', but this dictionary requires a
model item of type
'System.Collections.Generic.IEnumerable
1[Review.Models.ProductViewModel]'.
任何帮助都会很棒。
您正在return查看要查看的 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();
return PartialView(productviewmodel);
查看结果:
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>
RenderAction
和RenderPartial
有区别。在第一个你调用动作,但在第二个,你直接调用局部视图。
所以你不能在RenderPartial
中传递productId
,而是需要传递List<ProductViewModel>
。同样在 RenderPartial
中,您需要提供部分视图名称,而不是操作名称。
我正在尝试在每个产品下创建用户评论,我使用了 Html.RenderAction
Html.RenderAction("ProductReviewTest", new { id = productids });
它工作正常,但需要 9.4 秒才能加载带有评论的产品页面,因此尝试 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; }
}
我的查看结果:
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]'.
任何帮助都会很棒。
您正在return查看要查看的 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();
return PartialView(productviewmodel);
查看结果:
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>
RenderAction
和RenderPartial
有区别。在第一个你调用动作,但在第二个,你直接调用局部视图。
所以你不能在RenderPartial
中传递productId
,而是需要传递List<ProductViewModel>
。同样在 RenderPartial
中,您需要提供部分视图名称,而不是操作名称。