šljaker
šljaker

Reputation: 7374

ASP.NET MVC2 and routing

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 }) %>

Upvotes: 2

Views: 376

Answers (2)

MarkKGreenway
MarkKGreenway

Reputation: 8764

<%: 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)

Upvotes: 4

ChrisF
ChrisF

Reputation: 137148

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.

Upvotes: 1

Related Questions