【问题标题】:How can I use Html.ActionLink to specify an exact URL with MVC3如何使用 Html.ActionLink 通过 MVC3 指定确切的 URL
【发布时间】:2011-06-28 21:24:23
【问题描述】:

我有以下路线:

        routes.MapRoute(
            "Power", // Route name
            "Power/{id}", // URL with parameters
            new 
            { 
                controller = "flood", 
                action = "index", 
                id = UrlParameter.Optional 
            }
        );

以及我调用的以下地址:

<a href="/Power/"  >

现在我想用这样的 Html.ActionLink 进行上述调用:

@Html.ActionLink("xxx", 
                "index",
                "flood",
                new { "Power" },
                null 
                )

它似乎不起作用,因为我收到一个错误“无效的匿名类型声明”,其中我有新的 {“Power”}。谁能给我一些建议,让我走上正确的道路。

我还希望能够通过另一个链接调用以下内容:

<a href="/Power/001"  >`

谢谢

ps。请注意我使用的是 MVC3。我理解从版本 1 > 2 > MVC3 更改的语法。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    使用RouteLink 而不是ActionLink

    @Html.RouteLink("xxx", "Power", new { id = "123" })
    

    或者,如果您使用 ActionLink 指定控制器和操作,并根据您的路由定义顺序,应该选择正确的路由:

    @Html.ActionLink("xxx", "index", "flood", new { id = "123" }, null)
    

    【讨论】:

      【解决方案2】:

      问题在于参数的声明。 这会正常工作:

      没有身份证:

      @Html.ActionLink("xxx", 
                  "index",
                  "flood")
      

      带身份证:

      @Html.ActionLink("xxx", 
                  "index",
                  "flood",
                  new { id =123 },
                  null 
                  )
      

      你声明了一个匿名类型,没有说属性的名字,只说值。

      【讨论】:

      • 为什么是id="Power"?那是路线的名称。
      • 对不起,我误解了你的问题,看看我的编辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 2021-03-08
      相关资源
      最近更新 更多