I’ve seen a lot topics with such a problem, but could not find mine. After this error it specifies a source:
@{
Line 4: ViewBag.Title = "Edit product: " + @Model.Name;
Line 5: Layout = "~/Views/Shared/_MasterLayout.cshtml";
Line 6:
}
It happens after I click on the name of my product from a View
@Html.ActionLink(item.Name, "Edit", new { item.ProductID })
The link becomes
http://localhost:31363/Master/Edit?ProductID=1
And I am not able to see my view, however, if I edit the link manually to
http://localhost:31363/Master/Edit/1
It works. So, what should I fix to make it work either in first way or automatically in the second one? I don’t have any special routes right now, it is a default one coming with Mvc4 application.
This should solve the problem:
Explanation
In your code, you are creating a link with a ProductID parameter because
is equal to:
Since there isn’t a parameter named ProductID in the default route, it is added to querystring. Also your Edit action probably has an integer parameter named id like this:
Since this parameter is not optional or nullable, request cannot be mapped to this action and you get the error. So parameter names matter because ASP.NET MVC can’t figure it out by itself.