Reputation: 10753
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!
Upvotes: 0
Views: 339
Reputation: 218732
@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
Upvotes: 2
Reputation: 1038810
<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.
Upvotes: 2
Reputation: 5534
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")
Upvotes: 0