how to use Html.RenderPartial with ViewModel

4.6k views Asked by At

i am trying to create user reviews under each product, i used Html.RenderAction

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

it works fine but it takes 9.4s to load the product page with the reviews, so tried Html.RenderPartial but gives error

my product view:

@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>

my review view model:

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

}

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

my review view:

   @model IEnumerable<MVCProduct.Models.ProductViewModel>

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

</table>

error:

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.IEnumerable1[Review.Models.ProductViewModel]'.

any help would be great.

3

There are 3 answers

2
ramiramilu On BEST ANSWER

There is a difference between RenderAction and RenderPartial. In the first you are calling action, but in second, you are directly calling partial view.

So you cannot pass productId in RenderPartial, instead you need to pass List<ProductViewModel>. Also in RenderPartial, you need to give partial view name, not the action name.

0
Aswartha On

you are returning a List of ProductViewModel to view. Instead use

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

1
joaoeduardorf On

ViewResult:

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

product view:

@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>