开发者

ASP.NET MVC2 and routing

开发者 https://www.devze.com 2023-01-09 10:02 出处:网络
I have the following route: routes.MapRoute( \"edit_product\",// Route name \"Product/Edit/{productId}\",// URL with parameters

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


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


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.

0

精彩评论

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