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.

5
  • 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<ProductViewModel>. Also in RenderPartial, you need to give partial view name, not action name. Commented Jun 9, 2015 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 ? Commented Jun 9, 2015 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. Commented Jun 9, 2015 at 13:50
  • Can I post the comment as answer? Commented Jun 9, 2015 at 13:50
  • yes sure , it will help someone Commented Jun 9, 2015 at 13:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hi, one more query, does it make any difference using PartialViewResult with return PartialView and ActionResult with return View while using Html.RenderPartial ?
Typically there is no difference between ViewResult and PartialViewResult, both render response stream.
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>

1 Comment

thank you @joaoeduardorf, but i guess you forget to mention to create partial views
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.