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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I missed the null parameter.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.