开发者

ASP.NET MVC 2 Action Method receives null arguments

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>

S开发者_StackOverflow社区o far so good. When I hover the mouse over the details link in the web browser, the link shows up as:

http://localhost:40733/RawMaterial/Details/R8517

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?


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)


+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" } );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜