Kōdo no musō-ka Kōdo no musō-ka - 1 month ago 5
ASP.NET (C#) Question

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

<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?

Answer

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)
Comments