views:

120

answers:

2

I have the following route:

routes.MapRoute(
    "edit_product",                                // Route name
    "Product/Edit/{productId}",                    // URL with parameters
    new { controller = "Product", action = "Edit", 
          productId = UrlParameter.Optional }      // Parameter defaults
);

Why does this code works:

<%: Html.ActionLink("Edit", "Edit", 
    new { controller = "Product", productId = product.ProductId }) %>

And this doesn't:

<%: Html.ActionLink("Edit", "Edit", "Product", 
    new { productId = product.ProductId }) %>
+1  A: 

The first is resolving to this overload

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues
)

There is no overload of ActionLink that takes three strings and an object. The nearest is this one that takes two strings and two objects:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)

so I wouldn't expect it to do what you want.

ChrisF
+4  A: 
<%: Html.ActionLink("Edit", "Edit", "Product", 
    new { productId = product.ProductId } , null) %>

You need the null parameter

Actionlink doesnt have (LinkText, Actionname, Controller, Parameters) but does have (LinkText, Actionname, Controller, Parameters, htmlAttributes)

Hurricanepkt
Thank you! I missed the null parameter.
šljaker