Reputation: 13778
I'm having a problem with an ASP.NET MVC application that I'm developing. I'm still fairly new at web development and in particular MVC, so please forgive me is this is a really stupid newbie mistake ;-)
I have a view that displays a list of products. Each product has a 'details' link that I want to link to a details view for that product. So, here's the relevant markup from the view:
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.StockCode.ToString() }) %>
<%= Html.ActionLink("Details", "Details", new { id=item.StockCode.ToString() })%>
</td>
<td>
<%= Html.Encode(item.StockCode) %>
</td>
<td>
<%= Html.Encode(item.Description) %>
</td>
</tr>
So far so good. When I hover the mouse over the details link in the web browser, the link shows up as:
R8517 is the stock code for the item. So, that's what I expect to see in my Action Method. Here's the Action Method:
//
// GET: /RawMaterial/Details/5
public ActionResult Details(string stockCode)
{
return View(repository.GetByStockCode(stockCode));
}
But... when the Action Method executes, the parameter (stockCode) is null.
Any thoughts?
Upvotes: 0
Views: 1497
Reputation: 26648
+1 to DM's answer, but you also have another option. You can register special route for your case. Something like this:
routes.MapRoute("RawMaterial", "RawMaterial/{action}/{stockCode}",
new { controller = "RawMaterial", action = "Index" } );
Upvotes: 1
Reputation: 1847
The parameter you need to accept shouldn't be "stockCode" is should be "id"
MVC is trying to model bind the "id=item.StockCode.ToString()" to a parameter that is named the same. Your action method should be:
public ActionResult Details(string id)
Upvotes: 4