HTML.ActionLink method
Solution 1
I think what you want is this:
ASP.NET MVC1
Html.ActionLink(article.Title,
"Login", // <-- Controller Name.
"Item", // <-- ActionMethod
new { id = article.ArticleID }, // <-- Route arguments.
null // <-- htmlArguments .. which are none. You need this value
// otherwise you call the WRONG method ...
// (refer to comments, below).
)
This uses the following method ActionLink signature:
public static string ActionLink(this HtmlHelper htmlHelper,
string linkText,
string controllerName,
string actionName,
object values,
object htmlAttributes)
ASP.NET MVC2
two arguments have been switched around
Html.ActionLink(article.Title,
"Item", // <-- ActionMethod
"Login", // <-- Controller Name.
new { id = article.ArticleID }, // <-- Route arguments.
null // <-- htmlArguments .. which are none. You need this value
// otherwise you call the WRONG method ...
// (refer to comments, below).
)
This uses the following method ActionLink signature:
public static string ActionLink(this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object values,
object htmlAttributes)
ASP.NET MVC3+
arguments are in the same order as MVC2, however the id value is no longer required:
Html.ActionLink(article.Title,
"Item", // <-- ActionMethod
"Login", // <-- Controller Name.
new { article.ArticleID }, // <-- Route arguments.
null // <-- htmlArguments .. which are none. You need this value
// otherwise you call the WRONG method ...
// (refer to comments, below).
)
This avoids hard-coding any routing logic into the link.
<a href="/Item/Login/5">Title</a>
This will give you the following html output, assuming:
article.Title = "Title"
article.ArticleID = 5
- you still have the following route defined
. .
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Solution 2
I wanted to add to Joseph Kingry's answer. He provided the solution but at first I couldn't get it to work either and got a result just like Adhip Gupta. And then I realized that the route has to exist in the first place and the parameters need to match the route exactly. So I had an id and then a text parameter for my route which also needed to be included too.
Html.ActionLink(article.Title, "Login", "Item", new { id = article.ArticleID, title = article.Title }, null)
Solution 3
You might want to look at the RouteLink()
method.That one lets you specify everything (except the link text and route name) via a dictionary.
Solution 4
I think that Joseph flipped controller and action. First comes the action then the controller. This is somewhat strange, but the way the signature looks.
Just to clarify things, this is the version that works (adaption of Joseph's example):
Html.ActionLink(article.Title,
"Login", // <-- ActionMethod
"Item", // <-- Controller Name
new { id = article.ArticleID }, // <-- Route arguments.
null // <-- htmlArguments .. which are none
)
Solution 5
what about this
<%=Html.ActionLink("Get Involved",
"Show",
"Home",
new
{
id = "GetInvolved"
},
new {
@class = "menuitem",
id = "menu_getinvolved"
}
)%>
Related videos on Youtube
Comments
-
Graviton about 4 years
Let's say I have a class
public class ItemController:Controller { public ActionResult Login(int id) { return View("Hi", id); } }
On a page that is not located at the Item folder, where
ItemController
resides, I want to create a link to theLogin
method. So whichHtml.ActionLink
method I should use and what parameters should I pass?Specifically, I am looking for the replacement of the method
Html.ActionLink(article.Title, new { controller = "Articles", action = "Details", id = article.ArticleID })
that has been retired in the recent ASP.NET MVC incarnation.
-
BlueRaja - Danny Pflughoeft over 13 yearsDocumentation, for anyone looking for it: msdn.microsoft.com/en-us/library/…
-
Rei Miyasaka over 11 years@Danny Thanks, was looking for it on Google when I ended up here.
-
-
Adhip Gupta over 15 yearsBut, doesn't this give out a URL like /Item/Login?id=5 ?
-
Ian Oxley about 15 yearsThis is just what I needed - I had forgotten to add the final null argument. Thanks.
-
MordechayS almost 15 yearsWhat's strange is if you miss out the last parameter, it appends for me ?Length=8 to the current action
-
Pure.Krome almost 14 years@Chris S - I know this is an old post, but the reason for the ?Length=8 is because you need to have a
, null
parameter AFTER yournew { ... }
... because if you check the overloads of that method, it's thinking your paramters are htmlArguments ... not route arguments. To use the correct method, u need to use the method that hasrouteArguments, htmlArguments
.. so just pass in null for that lasthtmlArgument
. The first piece of code in this reply has it. I've updated this post so you can see that easily (ie. it doesn't scroll). -
Steve Duitsman over 13 yearsHas anyone tried this with MVC 3? It seems that the ControllerName and ActionMethod lines in the sample above are flipped. Anyone else seen that?
-
Pure.Krome over 13 years@Steve Duitsman - confirmed. It has flipped indead. It happened with ASP.NET MVC 2 ...
-
Gavin Coates about 13 yearsIn MVC3 the id property is not found... the following should be used instead:
@Html.ActionLink("Text","Action","Controller", new { item.ID }, null)
-
William Rose almost 13 yearsThanks for showing the mapping from route parameter name, too (e.g. new { id = ..., bar = ... }.
-
Scott Rippey over 12 years@JosephKingry I'm sorry, I know this post is old, but for this method, there are NO differences between MVC 1, 2, and 3. MVC 1 does NOT have the
action
andcontroller
switched, and MVC 3 still requires correctly named Route arguments. If I am mistaken, please point me to some documentation. -
Yes - that Jake. almost 12 yearsI've come back to this question and answer at least three times in my work. I wish I could upvote it more than once.
-
Shimmy Weitzhandler over 11 yearsWhat's the difference between
Html.Action
andHtml.ActionLink
? -
Simon Martin about 11 yearsWould be great to see an example of how that solves the issue; the MSDN page has lots of overloads and knowing what to look for could be confusing
-
Tom about 11 yearsCan someone tell me why the resulting HTML of this ActionLink is including the
Index
action in the link? How can I make this default?@Html.ActionLink(item.Name, "Index", "Forum", new { id = item.Name }, null)
I want to do this with passingid
as a string (which is working... just want to simplify/Forum/Index/StringId
to/Forum/StringId
. My routeConfig is set as it would be after File > New Project -
John almost 11 years@Adhip This will give out as you stated.. To get it in the form Item/Login/5 you have to change the route arguments from "new { article.ArticleID }" to new {id = article.ArticleID }. Your action method parameter will also have to be named "id". If so the default route will pick it up.
-
Indy-Jones almost 10 yearsThis really should have been marked as the answer since it does exactly what the person asking the question was looking for...however I will note that the marked answer did go into a great detail for the user in correctly setting up routes in various versions of MVC.
-
Johnathon Sullinger over 9 yearsI'm surprised more don't use this approach. It seems really dangerous to use string literals all over in your views to represent a controller/action.
-
ThisGuy over 9 yearsI recommend using named parameters in this case, so everyone (the programmer and compiler) are clear on what is what:
@Html.ActionLink(article.Title, actionName: "Item", controllerName: "Login", routeValues: new { article.ArticleID }, htmlAttributes: null)
-
Worthy7 over 7 yearsBeen looking for this all my life
-
Worthy7 over 7 yearsTried this, didn't work. Gave me a blank string in the end - I assume because I have parameters in my functions.
-
Serj Sagan over 7 yearsCan you post a github or other place with this code so I can take a look and see why it's not working for you?
-
gdbj over 6 yearsNice use of the word fancypants. We don't see that enough.
-
Atif about 3 yearsHtml.ActionLink(article.Title, "Item", // <-- ActionMethod "Login", // <-- Controller Name. new { Id = article.ArticleID } )
-
MaxPayne over 2 yearsCheck this: codingfusion.com/Post/…