2

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

        <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

I want the URL, when clicked to be /Staff/Index/theUnitId

Can someone help me?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Kōdo no musō-ka
  • 769
  • 1
  • 8
  • 23

3 Answers3

3

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)
JuanR
  • 7,405
  • 1
  • 19
  • 30
  • 1
    On the right track, but "Controller" still shouldn't be part of the `controllerName` parameter, unless your controller's class name is `StaffControllerController`. – jmoerdyk Oct 27 '16 at 17:38
  • Ah yes, silly me, I also had to change, "StaffController" to just "staff". Thank you so much! =) – Kōdo no musō-ka Oct 27 '16 at 17:40
  • Yes, thank you @jmoerdyk. I just copied and pasted what he had. Multi-tasking here. I will edit the answer! – JuanR Oct 27 '16 at 17:41
1

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 :

@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 :

@Html.ActionLink("View Staff", "Index", "Staff" , new { id =  item.UnitCode }, null) 
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

I think this answer can help you. Please see ASP.NET MVC3+ section which says:

ASP.NET MVC3+

arguments are in the same order as MVC2, however the id value is no longer required:

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.

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".

Third, your problem may be caused by different Routing configuration in RouteConfig class.

Community
  • 1
  • 1
kat1330
  • 5,134
  • 7
  • 38
  • 61
  • Instead of posting links as answer add some text to explanation how this answer help to OP in fixing current issue.Thanks – Roman Marusyk Oct 27 '16 at 22:41