0

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.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • 4
    you need to understand the 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`. Also in `RenderPartial`, you need to give partial view name, not action name. – ramiramilu Jun 09 '15 at 13:21
  • @ramiramilu, thank you so much, i created a PartialView by right click add view, checked create partial view and now it works, ok which is fast Html.RenderPartial or Html.RenderAction ? – Shaiju T Jun 09 '15 at 13:48
  • It depends, if you are using `RenderAction`, you are calling the same linq query again and again which is costly because of DB hits. But if you want to get all items at once and then use `RenderPartial` then you can avoid most of the DB hits. – ramiramilu Jun 09 '15 at 13:50
  • Can I post the comment as answer? – ramiramilu Jun 09 '15 at 13:50
  • yes sure , it will help someone – Shaiju T Jun 09 '15 at 13:52

3 Answers3

2

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.

ramiramilu
  • 17,044
  • 6
  • 49
  • 66
  • hi, one more query, does it make any difference using `PartialViewResult` with `return PartialView` and `ActionResult` with `return View` while using `Html.RenderPartial` ? – Shaiju T Jun 09 '15 at 14:11
  • Typically there is no difference between `ViewResult` and `PartialViewResult`, both render response stream. – ramiramilu Jun 09 '15 at 16:26
1

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>
joaoeduardorf
  • 44
  • 1
  • 3
0

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

Aswartha
  • 56
  • 9