ActionLink 参数始终为 null - 即使使用自定义路由

ActionLink Parameters always null - even with custom routing

应用程序管理请求。有一个 ActionLink 应该传入请求的 id,以便它可以被重置。调用了正确的controller/action,但请求ID始终为空。

如果我将自定义路由(如下所示)的最后一个参数更改为 id="TEST",然后 "TEST" 会传递到函数中 - 所以我知道选择了正确的路由。

在控制器中BrowseController.cs

 [HttpGet]
 public ActionResult ResetRequest(string id )
 {
   return View();
 }

在视图 BrowseRequests.cshtml 中有一个 link 重置请求

    @Html.ActionLink(
    "Reset", 
    "ResetRequest", 
    "Browse", 
    new {id = item.RS_RequestID.ToString() });

我试过 RouteConfig.cs 中的默认路由,然后尝试在默认路由之前插入以下内容。

    routes.MapRoute(
       name:"ResetRequest",
       url:"Browse/ResetRequest/{id}",
       defaults: new { controller = "Browse", 
                       action = "ResetRequest", 
                       id=UrlParameter.Optional});

您传递给 ActionLink 的值的顺序不正确

使用操作、控制器、routevalues、htmlArguments

您的第一个参数应该是 ResetRequest

您的建议导致 "resource not found" 错误。这确实让我想到,也许我为 ActionLink 使用了不正确的签名。

我发现了一个未指定控制器的覆盖 - 仅指定了操作。 这就是我的目的

    @Html.ActionLink(
      "Reset", 
      "ResetRequest", 
      new { id = item.RS_RequestID.ToString() });

您的 ActionLink 的正确重载是

HtmlHelper.ActionLink(string linkText,string actionName,string ControllerName, object routeValues, object htmlAttributes);

所以您缺少可以作为 null 传递的对象 htmlAttributes

@Html.ActionLink("Reset","ResetRequest","Browse", new {id = item.RS_RequestID.ToString() }, null);