简体   繁体   中英

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

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. :)

Is this the overload you require? You will need the 5th parameter for html attributes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM