【问题标题】:Route parameters coming through null路由参数通过 null
【发布时间】:2013-12-17 23:08:36
【问题描述】:

不确定我在这里做什么,但任何和所有参数都以null 的形式传入我的控制器,即使它们在呈现的 HTML 中已明确定义。

查看

    @Html.ActionLink("Export to Spreadsheet", "Export", "ZipCodeTerritory"
    , new {
       @searchZip = Model.searchZip,
       @searchActiveOnly = Model.searchActiveOnly,
       @searchTerritory = Model.searchTerritory,
       @searchState = Model.searchState
    })

控制器

    public ActionResult Export(string searchZip, bool? searchActiveOnly, string searchTerritory, string searchState)
    {

呈现的 HTML

<a href="/ZipCodeTerritory/Export?Length=16" searchactiveonly="True" searchstate="CA" searchterritory="" searchzip="">Export to Spreadsheet</a>

【问题讨论】:

  • 参数字典中的 @ 不是必需的,这可能是导致此问题的原因。您呈现的 HTML 对于 URL 来说非常糟糕,这意味着它是 ActionLink 帮助程序无法正常运行。看来 ActionLink 正在将您的路由参数更改为 &lt;a&gt; 标记属性。尝试删除 ActionLink 的 new {} 部分中的 @
  • 删除了它们,但仍然没有运气。还注意到关于呈现的链接,但是删除 @ 并没有改变任何东西。

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


【解决方案1】:

您为 Html.ActionLink 使用了错误的重载。它认为您的路线值实际上是 html 属性。此外,您需要删除每个变量名称中的“@”。尝试改变这个:

@Html.ActionLink("Export to Spreadsheet", "Export", "ZipCodeTerritory"
, new {
   @searchZip = Model.searchZip,
   @searchActiveOnly = Model.searchActiveOnly,
   @searchTerritory = Model.searchTerritory,
   @searchState = Model.searchState
})

到这里:

@Html.ActionLink("Export to Spreadsheet", "Export", "ZipCodeTerritory"
, new {
   searchZip = Model.searchZip,
   searchActiveOnly = Model.searchActiveOnly,
   searchTerritory = Model.searchTerritory,
   searchState = Model.searchState
}, null)

【讨论】:

  • 另外,可以通过使用 Html.ActionLink(LinkTExt, Action, RouteValues) 重载将控制器移动到 RouteValue 字典中,然后不需要 null
【解决方案2】:

它使用了错误的重载,试试这个:

@Html.ActionLink("Export to Spreadsheet", "Export", "ZipCodeTerritory"
    , 
     new {
       searchZip = Model.searchZip,
       searchActiveOnly = Model.searchActiveOnly,
       searchTerritory = Model.searchTerritory,
       searchState = Model.searchState
    }
    , null)

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2020-04-19
    相关资源
    最近更新 更多