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.
Comments
Anonymous
November 10, 2010
Exactly what I was looking for. Thanks!Anonymous
August 17, 2011
It took me a couple of hours to find this. Thank you very much. Even if the documentation is poor, the ActionLink helper should have been coded better to not confuse people like this.Anonymous
August 17, 2011
Cool, thanks for the feedback, glad I spent the time to write it down :-)Anonymous
December 14, 2011
Perfect, just what I was looking for. Thanks.Anonymous
January 09, 2012
Lived with a work around to the default controller and now reverting to the desired controller as suggested. Works great. Thanks.Anonymous
April 17, 2012
Thanks for taking the time to post - it saved my sanityAnonymous
January 30, 2013
thanks for sharing, yeah, you are the first to speak outAnonymous
September 16, 2014
Thank for the information. Many other resources could not provide this information, but this was exactly what I needed.Anonymous
December 22, 2015
After 5 years of your blogging, this is very useful reference! Saved me a lot of time!Anonymous
January 16, 2016
Thanks for sharing such type of information