HTML.ActionLink method

777,261

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:

  1. article.Title = "Title"
  2. article.ArticleID = 5
  3. 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" 
                       }
                   )%>
Share:
777,261

Related videos on Youtube

Graviton
Author by

Graviton

A software developer.

Updated on April 18, 2020

Comments

  • Graviton
    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 the Login method. So which Html.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
      BlueRaja - Danny Pflughoeft over 13 years
      Documentation, for anyone looking for it: msdn.microsoft.com/en-us/library/…
    • Rei Miyasaka
      Rei Miyasaka over 11 years
      @Danny Thanks, was looking for it on Google when I ended up here.
  • Adhip Gupta
    Adhip Gupta over 15 years
    But, doesn't this give out a URL like /Item/Login?id=5 ?
  • Ian Oxley
    Ian Oxley about 15 years
    This is just what I needed - I had forgotten to add the final null argument. Thanks.
  • MordechayS
    MordechayS almost 15 years
    What's strange is if you miss out the last parameter, it appends for me ?Length=8 to the current action
  • Pure.Krome
    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 your new { ... } ... 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 has routeArguments, htmlArguments .. so just pass in null for that last htmlArgument. 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
    Steve Duitsman over 13 years
    Has 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
    Pure.Krome over 13 years
    @Steve Duitsman - confirmed. It has flipped indead. It happened with ASP.NET MVC 2 ...
  • Gavin Coates
    Gavin Coates about 13 years
    In MVC3 the id property is not found... the following should be used instead: @Html.ActionLink("Text","Action","Controller", new { item.ID }, null)
  • William Rose
    William Rose almost 13 years
    Thanks for showing the mapping from route parameter name, too (e.g. new { id = ..., bar = ... }.
  • Scott Rippey
    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 and controller switched, and MVC 3 still requires correctly named Route arguments. If I am mistaken, please point me to some documentation.
  • Yes - that Jake.
    Yes - that Jake. almost 12 years
    I'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
    Shimmy Weitzhandler over 11 years
    What's the difference between Html.Action and Html.ActionLink?
  • Simon Martin
    Simon Martin about 11 years
    Would 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
    Tom about 11 years
    Can 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 passing id 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
    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
    Indy-Jones almost 10 years
    This 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
    Johnathon Sullinger over 9 years
    I'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
    ThisGuy over 9 years
    I 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
    Worthy7 over 7 years
    Been looking for this all my life
  • Worthy7
    Worthy7 over 7 years
    Tried this, didn't work. Gave me a blank string in the end - I assume because I have parameters in my functions.
  • Serj Sagan
    Serj Sagan over 7 years
    Can 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
    gdbj over 6 years
    Nice use of the word fancypants. We don't see that enough.
  • Atif
    Atif about 3 years
    Html.ActionLink(article.Title, "Item", // <-- ActionMethod "Login", // <-- Controller Name. new { Id = article.ArticleID } )
  • MaxPayne
    MaxPayne over 2 years