Search code examples
asp.net-mvc-4navigationhtml-helperhtml.actionlink

calling different Action Method from another Controller


I have a default Controller HomeController.cs having a ActionLink. I have a second controller StudentController.cs having several ActionMethods. I am trying to call the Student controller's GetStudent ActionMethod from Home controller's view but I get following error:

Server Error in '/' Application. The resource cannot be found.

Home Controllers view code where I am using HTML Helper ActionLink to navigate

<p>@Html.ActionLink("Get Student Info", "Main", "Student")</p>

Solution

  • If the method of StudentController is named GetStudent, then it needs to be

    @Html.ActionLink("Get Student Info", "GetStudent", "Student")
    

    The 2nd parameter is the name of the method your calling.