2

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

2 Answers 2

22

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer. Is it something new in MVC3? In MVC2 I remember it worked as what I attempeted to do (the overload in my question)
AFAIK, it should be the same in MVC2. :)
Doesn't seem to work if there is an action in the current controller with the same name
0

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" )

2 Comments

thanks for your answer. Whay shall I use html attributes? Your overload will just specify an HTML name attribute with the "download" value( <a name="download", ...>. Besides being useful for binding I do not see the reason to use that
Because that was this the overload that supported specifying all the parameters you wanted. You may just be able to use null as the final parameter but I wasn't certain so I just put null. Failing that give it an empty dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.