Html.ActionLink to call an action from a different controller that's in a different folder

SantasNotReal picture SantasNotReal · Jul 13, 2013 · Viewed 21.2k times · Source

I've got a simple view that creates a link if a login is successful and is located under /Login:

<div>
@Html.ActionLink("Add a new Organization", "AddOrganization", 
           "/Setup/AddOrganizationController", new { id = Session["ID"] }, null)
</div>

After reading other similiar problems, I tried it adding the null after, as well as a few other overloads, but I can't get the link to work right. When I click the link, it takes me to

http://setup/AddOrganizationController/AddOrganization

Which is leaving out the localhost part that needs to be there. Without the null at the end, it tries to send me to

/Login/AddOrganization

All I want is a link that will run an action within the AddOrganizationController controller which is under /Setup directory. The link should also pass the session id to the controller as an argument. How can I do this?

Answer

Matt Millican picture Matt Millican · Oct 7, 2013

If it's in the same Area then you can just do:

@Html.ActionLink("Add a new Organization", "AddOrganization", "Organizations", new { id = Session["ID"] })

where "Organizations" is the controller name.

Otherwise, if it's in another area you would do something like

@Html.ActionLink("Add a new Organization", "AddOrganization", "Organizations", new { area = "areaName", id = Session["ID"] }, null)