Home > ASP.NET MVC > ASP.NET # MVC # 17 – Html.ActionLink and Html.RouteLink Helper Methods

ASP.NET # MVC # 17 – Html.ActionLink and Html.RouteLink Helper Methods

 

Hi Geeks,

Today we will see Html.ActionLink and Html.RouteLink Helper methods.

I )Html.ActionLink

** The ActionLink method renders a hyperlink (anchor tag) to another Controller action. This method uses the Routing API under the hood to generate the URL. For example, when linking to another action within the same Controller used to render the current View, you can simply specify the action name:

     <%: Html.ActionLink("Link Text", "AnotherAction") %>

     This produces the following markup, assuming the default routes:

     <a href=”/Home/AnotherAction”>LinkText</a>

You can specify a Controller name as the third argument to link to an action of another Controller.For example, to link to the AccountController.Withdraw action, use:

<%: Html.ActionLink("Link Text", "Withdraw", "Account") %>

 

Note :Notice that you specify the Controller name without the Controller suffix. You never specify the Controller’s type name.
In many cases, you have more route parameters than you need to supply in order to choose the correct URL to render for the action link. In that case, you can specify a RouteValueDictionary for the routeValues argument. The following example uses the overload that accepts the anonymous object overload for route values to specify the ID:

 <%: Html.ActionLink("Link Text", "Withdraw", "Account", new {id=34231}, null) %>

** To specify HTML attributes, provide a value for the htmlAttributes argument. This accepts an IDictionary<string, object>, but for ease of use, you can call the overload that accepts an anonymous object:

<%: Html.ActionLink("LinkText", "About", "Account", null,
new {title="withdraw from account"}) %>

II) Html.RouteLink

The ActionLink methods have specific knowledge about ASP.NET MVC Controllers and actions.
Hence, these methods provide convenient overloads that allow the specifying of a controllerName and actionName directly.

The RouteLink methods follow the same pattern as the ActionLink methods but also accept a
route name and do not have arguments for Controller name and action name. For example, the first example ActionLink shown previously is equivalent to the following:

<%: Html.RouteLink("Link Text", new {action="AnotherAction"}) %>

(REFERENCE – Professional.ASP.NET.MVC.2 Wrox publication)

Advertisements
Categories: ASP.NET MVC
  1. December 18, 2012 at 12:09 am

    Hi i am kavin, its my first occasion to commenting anywhere, when i read this article
    i thought i could also create comment due to this brilliant
    post.

  2. April 29, 2013 at 4:47 pm

    I seldom leave a response, however i did a few searching and wound up here ASP.
    NET # MVC # 17 – Html.ActionLink and Html.RouteLink Helper Methods | Microsoft Mentalist.
    And I do have 2 questions for you if you don’t mind. Could it be only me or does it look like a few of these responses come across like they are left by brain dead visitors? 😛 And, if you are posting on other online social sites, I would like to follow anything fresh you have to post. Could you make a list of the complete urls of all your communal pages like your twitter feed, Facebook page or linkedin profile?

  3. May 13, 2013 at 2:39 am

    Ahaa, its fastidious conversation about this piece of writing here at this weblog, I have read all that, so at this time me
    also commenting here.

  4. May 31, 2013 at 1:04 am

    Attractive section of content. I just stumbled upon your blog and in accession capital to
    assert that I acquire in fact enjoyed account your blog posts.
    Anyway I’ll be subscribing to your feeds and even I achievement you access consistently fast.

  5. September 24, 2013 at 2:54 am

    Hi there! Do you use Twitter? I’d like to follow you if that would be
    ok. I’m definitely enjoying your blog and look forward to new posts.

  6. February 11, 2014 at 7:21 am

    Wow that was unusual. I just wrote an incredibly long comment but after
    I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over again.

    Anyway, just wanted to say fantastic blog!

  7. April 11, 2014 at 3:11 am

    Touche. Outstanding sprzątanie arguments. Continue the good work.

    This particular post sprzątanie is priceless. Where will
    I garner more information?

  8. May 1, 2015 at 1:19 am

    I think that is among the most vital info for me. And i am glad studying your article.
    However wanna observation on some normal things, The site taste is ideal, the articles is actually excellent :
    D. Good process, cheers

  1. March 23, 2012 at 1:43 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: