tags:

views:

1302

answers:

3

I am trying to navigate between controllers using ActionLink.

I will tell my problem with an example.

I am on Index view of Hat controller

I am trying to use below code to create a link to Details action of Product controller.

<%= Html.ActionLink("Details", "Details", "Product", new { id=item.ID }) %>

Instead of creating me a link to Details on Product controller this generates a link to Details action under Hat controller and appends a Length parameter to the end of it:

Hat/Details/9?Length=7

I am not able to use HTML.ActionLink to switch between controllers because of this problem. I will appreciate if you can point me to what I am doing wrong. Thanks

PS: I am using the default route setting that comes with mvc

routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } );

+2  A: 

You're hitting the wrong the overload of ActionLink. Try this instead.

<%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>
Craig Stuntz
+3  A: 

If you grab the MVC Futures assembly (which I would highly recommend) you can then use a generic when creating the ActionLink and a lambda to construct the route:

<%=Html.ActionLink<Product>(c => c.Action( o.Value ), "Details" ) %>

You can get the futures assembly here: http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471

James Avery
Thanks for the actual link - was trying to find it!
Perhentian
Careful with this though as it's not been included in MVC2. The reasoning is that Actions are not necessarily Methods (which I agree with but it's going to be a pain to migrate as we've been using the generic method for a while now). Here's the full article on why it's not been kept in 2: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx
Stu
+9  A: 

What you want is this overload :

//linkText, actionName, controllerName, routeValues, htmlAttributes
<%=Html.ActionLink("Details", "Details", 
    "Product", new {id = item.ID}, null) %>
çağdaş
Can you explain why this particular overload works and korki's does not? How does setting htmlAttributes to null affect the routing of the link?
Derek Hunziker
@Derek Hunziker, Simply the arguments are different for korki's overload.
çağdaş