Search code examples
c#asp.net-mvc-3actionlinkroutevalues

How to specify controller in ActionLink html helper in C#


I have a web application developed in ASP.NET MVC3 with C# and Razor.

I would like to call a specific Action Method of a specific Controller by using the ActionLink HTML helper. I know that the second parameter of ActionLink specifies the Action Method to be called from the Default route, which is the only one in my Global.asax file:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Index", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

I would like to call the Download Action Method from the Home Controller instead of Index. This does not work:

@Html.ActionLink("Presentation", "Download", "Home", new { topicId = topic.TopicId } )

It requires as third parameter a type Object but I cannot find on the web any example.

What are the steps needed in order to call a specific Controller/ActionMethod? Shall I create another route in my Global.asas file?

Thanks


Solution

  • Try this one:

    @Html.ActionLink("Download", "Download", new { controller = "Home",  Id = topic.TopicId });
    

    The third parameter, object: routeValues, is used as dictionary in Asp.net MVC. Phil Haacked blogged about the decision for using object as route values.

    update:
    Your overload function is not working because you are calling this method. String is also object. So, you are passing "Home" as routeValues and new { topicId = topic.Id} as htmlAttributes. :)