0

I am building a website where there are products and you can leave reviews on products. I have a link on the product page that says "Leave a review"

In my code it looks like this:

@Html.ActionLink("Leave a Review", "AddReview", "Product", new { id = Model.ProductId }, null) 

When I run my application the link works fine, but when I submit a review it crashes and says:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Product(System.String, Int32)' in 'MyProject.Controllers.ProductController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

This error only occurs if I have new { id = Model.ProductId } in my ActionLink.

This may seem like the ProductId is null, but that isn't possible. To load up the product page in the first place a ProductId is needed, plus I use a RenderAction that displays reviews about that product on the page using that same id:

@{Html.RenderAction("Reviews", "product", new { id = Model.ProductId});} 

So basically, why am I getting this error? Does it have something to do with there being a GET and POST AddReview method? I don't think it should considering the link would pull the GET and when I submit the form it should be a POST. How do I get the productId into the form?

EDIT To answer some questions:

My form looks like this:

@using (Html.BeginForm("AddReview", "Product")) { <p>Title @Html.TextBoxFor(x => x.Title)</p> <p>Rating: @Html.TextBoxFor(x => x.Rating)</p> <p>Body: @Html.TextBoxFor(x => x.Body)</p> <input type="submit" value="Save"/> Html.ActionLink("Cancel", "Index", "Home"); } 

My generated tag looks like this:

<a href="/Product/AddReview?ProductId=9">Leave a Review</a> 

EDIT

After having fixed the link issue I realized that the reason I was still getting an error was because of a RedirectToAction call was broken in my controller. What a stupid mistake!

7
  • 1
    What does the code to create the form look like? Commented Apr 16, 2012 at 16:30
  • How does the <a> tag look in the generated HTML when you browse the source? Commented Apr 16, 2012 at 16:35
  • I have added both of these to my original post. Commented Apr 16, 2012 at 16:37
  • Can we see the actions in your ProductController that you are using? It feels to me as if you think you are passing one parameter, but you're instead sending another. I would like to see what's going on in your controller. Commented Apr 16, 2012 at 16:55
  • I fixed it. Shyju's answer fixed my link and then I realized that something else was broken in my controller. Commented Apr 16, 2012 at 17:05

3 Answers 3

2
@Html.ActionLink("Leave a Review", "AddReview", "Product", new { @id = Model.ProductId }, null) 

This should generate a link like below if you have a valid valie in your ProductId property for this model

<a href="/Product/AddReview/9">Leave a Review</a> 

Make sure you have the ProductId Property value loaded properly.

you may use @ symbol as a prefix to parameters which has the same name as of HTML attribues. See i used @id instead of id

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

2 Comments

This generates the proper link. I still get the error and I suspect that's because I'm not loading the value into my form properly...
And yet the review still posts.
2
<a href="/Product/AddReview?ProductId=9">Leave a Review</a> 

is not good. You should get:

<a href="/Product/AddReview/9">Leave a Review</a> 

or:

<a href="/Product/AddReview?id=9">Leave a Review</a> 

because your controller action expects a non-nullable int parameter called id. The reason you are getting this wrong url might be because you have messed up with your routes.

3 Comments

I haven't touched my routes though. It's the default {controller}/{action}/{id} setup.
Maybe it's your form then. Have you tried including the route parameter: @using (Html.BeginForm("AddReview", "Product", new { id = Model.ProductId }, FormMethod.Post))?
So, I get the error but the reviews still post. That's silly.
0

It sounds like you are not setting the ProductId in the form. You either need to set the form action to include the id in the route: @Html.BeginForm("AddReview","Product", new {Id = Model.ProductId}) or include a hidden input for the productId, e.g. @Html.Hidden("id",Model.ProductId")

3 Comments

How do I pass the ID into the form then?
@DarinDimitrov, it is better to pass the ID in the form action or in a hidden? Pros and cons...
@Romias I think the form value wins if they are different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.