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

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


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


在控制器 BrowseController.cs


 [HttpGet]

 public ActionResult ResetRequest(string id )

 {

   return View();

 }

在 View BrowseRequests.cshtml 中有一个重置请求的链接


    @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});


慕妹3146593
浏览 90回答 3
3回答

慕森卡

您的 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);

猛跑小猪

您的建议导致“找不到资源”错误。这确实让我想到,也许我为 ActionLink 使用了不正确的签名。我发现了一个未指定控制器的覆盖 - 仅指定了操作。这对我的目的有用    @Html.ActionLink(      "Reset",       "ResetRequest",       new { id = item.RS_RequestID.ToString() });

开满天机

您传递给 ActionLink 的值的顺序不正确使用操作、控制器、路由值、htmlArguments你的第一个参数应该是 ResetRequest
打开App,查看更多内容
随时随地看视频慕课网APP