【问题标题】:ASP.NET MVC2 and routingASP.NET MVC2 和路由
【发布时间】:2010-07-26 15:23:23
【问题描述】:

我有以下路线:

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

为什么这段代码有效:

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

这不是:

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

【问题讨论】:

    标签: asp.net-mvc-2 routing


    【解决方案1】:
    <%: Html.ActionLink("Edit", "Edit", "Product", 
        new { productId = product.ProductId } , null) %>
    

    你需要空参数

    Actionlink 没有(LinkText、Actionname、Controller、Parameters)但有 (LinkText、Actionname、Controller、Parameters、htmlAttributes)

    【讨论】:

    • 谢谢!我错过了 null 参数。
    【解决方案2】:

    第一个解析为this overload

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

    ActionLink 没有使用三个字符串和一个对象的重载。最接近的是this one,它接受两个字符串和两个对象:

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

    所以我不希望它做你想做的事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      相关资源
      最近更新 更多