Share via


Calling a Different Controller with Html.ActionLink

I have been working away on the web site but haven’t had too much time to blog about it.  I have a few thoughts and fixes here and there which I do need to record, though.  The first of them is the Html.ActionLink helper. 

My main entities are Members and Runs.  However, the two are linked in a number of ways so it is inevitable that from the context of a Member, I need to update some Run information or vice versa.  For example, in my member transcript page, which records the runs they have done, or are setting, the member is able to update some details about some Runs.  So from a Member view invoked from the Members controller, I want to invoke a method in the Runs controller.  It took me a while to figure out the correct signature for ActionLink, and here it is:

 <%= Html.ActionLink("Details", "Details", "Runs", new { id = rh.RunNumber }, null) %>

It turns out the null at the end of the signature is important.  The third parameter is the controller I am calling, the second is the method, and the first is of course the link text.  Without the null at the end, this helper generates some bizarre link within the context of the controller that invoked the current view.  I think the documentation needs to be a little more clear.  As usual, I found this fix pretty easily with a web search.