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!
<a>tag look in the generated HTML when you browse the source?