开发者

How to specify controller in ActionLink html helper in C#

开发者 https://www.devze.com 2023-03-22 23:57 出处:网络
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 secon

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
            "{co开发者_如何转开发ntroller}/{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" )
0

精彩评论

暂无评论...
验证码 换一张
取 消