简体   繁体   English

ASP.Net MVC5 HTML.ActionLink到不同控制器中的索引方法

[英]ASP.Net MVC5 Html.ActionLink to index method in different controller

I have a ActionLink which when clicked, i want to pass an id into the index method of another controller and take the user to that controllers index page 我有一个ActionLink,当单击该链接时,我想将ID传递到另一个控制器的index方法中,并将用户带到该控制器的索引页面

        <td>
        @Html.ActionLink("View Staff", "Index", "StaffController" , new { id =  item.UnitCode }) |

However, when i click this link, the page simple stays the same and the URL changes to /Units?Length=15 但是,当我单击此链接时,页面简单保持不变,URL更改为/Units?Length=15

I want the URL, when clicked to be /Staff/Index/theUnitId 我希望单击该网址时是/Staff/Index/theUnitId

Can someone help me? 有人能帮我吗?

I believe your call is matching the wrong overload of the helper method. 我相信您的呼叫与辅助方法的错误重载匹配。

As written, it will match this signature: 按照书面规定,它将匹配以下签名:

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

Notice there is no controller in there. 请注意,那里没有控制器。

Try this instead: 尝试以下方法:

@Html.ActionLink("View Staff", "Index", "Staff" , new { id =  item.UnitCode }, null)

Which should match the right signature with controller: 哪个应该与控制器匹配正确的签名:

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

Check Your Controller Name 检查您的控制器名称

Firstly, unless your controller is named StaffControllerController , you'll likely want to adjust the name of the controller parameter in your ActionLink() helper method : 首先,除非您的控制器名为StaffControllerController ,否则您可能需要在ActionLink()帮助器方法中调整控制器参数的名称:

@Html.ActionLink("View Staff", "Index", "Staff", ... );

Ensure Proper Overload Arguments 确保正确的重载参数

Additionally, the overload that you are attempting to use requires both a RouteValues parameter (which you are using) along with an htmlAttributes parameter, which can be null as seen below : 此外, 您尝试使用的重载同时需要一个RouteValues参数(您正在使用)和一个htmlAttributes参数,该参数可以为null,如下所示:

@Html.ActionLink("View Staff", "Index", "Staff" , new { id =  item.UnitCode }, null) 

I think this answer can help you. 我认为这个答案可以为您提供帮助。 Please see ASP.NET MVC3+ section which says: 请参阅ASP.NET MVC3 +部分,其中指出:

ASP.NET MVC3+ ASP.NET MVC3 +

arguments are in the same order as MVC2, however the id value is no longer required: 参数与MVC2的顺序相同,但是不再需要id值:

 Html.ActionLink(article.Title, "Item", // <-- ActionMethod "Login", // <-- Controller Name. new { article.ArticleID }, // <-- Route arguments. null // <-- htmlArguments .. which are none. You need this value // otherwise you call the WRONG method ... // (refer to comments, below). ) 

It looks like that you are not supplied correct overloading for this method. 看来您没有为此方法提供正确的重载。

At first, please use action link with following arguments: 首先,请使用带有以下参数的操作链接:

 @Html.ActionLink("View Staff", "Index", "StaffController" , new { id =  item.UnitCode }, null)

Not last argument which htmlArguments should be null. 不是最后一个参数,哪个htmlArguments应该为null。

Second, maybe in yuor action link third argument which represents controller name is wrong! 第二,也许在您的动作链接中代表控制器名称的第三个参数是错误的! If is name of your controller class "StaffController" than yuo should use argument "Staff", not "StaffController". 如果是控制器类“ StaffController”的名称,那么您应该使用参数“ Staff”,而不是“ StaffController”。

Third, your problem may be caused by different Routing configuration in RouteConfig class. 第三,您的问题可能是由R​​outeConfig类中的不同路由配置引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM