tags:

views:

21073

answers:

7

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 to pass in?

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.

A: 

Html.ActionLink(article.Title, article.ArcticleID, "Articles/Details")

alexmac
A: 
Html.ActionLink(article.Title, "Login/" + article.ArticleID, 'Item")
Adhip Gupta
This works for me...
zidane
It's Work for me to
Cédric Boivin
+36  A: 

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)

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, assmuming:

  1. article.Title = "Title"
  2. article.aritcleID = 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
);
Joseph Kingry
But, doesn't this give out a URL like /Item/Login?id=5 ?
Adhip Gupta
NO this will give the following route:http://localhost:23445/Item/Login/5
Joseph Kingry
What's strange is if you miss out the last parameter, it appends for me ?Length=8 to the current action
Chris S
@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).
Pure.Krome
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?
Steve Duitsman
@Steve Duitsman - confirmed. It has flipped indead. It happened with ASP.NET MVC _2_ ...
Pure.Krome
+7  A: 

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.

Haacked
+2  A: 

I can't comment yet but 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)

Jeff Widmer
This is just what I needed - I had forgotten to add the final *null* argument. Thanks.
Ian Oxley
A: 

I think that Joseph flipped controller and action. First comes the action than 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
            )
agez
A: 

what about this

<%=Html.ActionLink("Get Involved", "Show", "Home", new { id = "GetInvolved" }, new { @class = "menuitem", id = "menu_getinvolved" })%>

Hasan