tags:

views:

241

answers:

2

I'm currently trying to user actionlink helpers in a way that I don't think was described in NerdDinner.

Lets say I am on this page

/Dinners/

and on that page there is a list of dinners, ok fine and working

now lets say I want to goto a new section of the site, which I have created a new controller MenuItemsController

so lets say I want to goto a new part of the website that manages menu items.

So going to

/menuitems/3

would bring up all the menu items assoicated with dinner id 3.

This is also working.

I am having trouble, linking to each of the menu item pages, because when I use the actionlink code, without much modification i get this

dinner1 = link /dinners/menuitems/3

rather than

dinner = link /menuitems/3

The actionlink code i am trying is

<%= Html.ActionLink("Menu Items", "/menuitems", new { id=item.id })%>

you can see the / there. This feels wrong.

I wasn't sure if this post was talking about the same problem or not.

http://stackoverflow.com/questions/1284885/how-do-i-have-links-to-root-controllers-in-site-master-in-asp-net-mvc

Are action links the completely wrong thing for me to be using here, becuase they are binded directly to the controller I am currently inside of?

If so, what would be the best method for me to achieve what I am trying to do, and also add further complexity like linking to create/edit/delete methods?

A: 

I oddly after much searching all day just found this page,

http://devlicio.us/blogs/derik%5Fwhittaker/archive/2008/03/06/link-building-101-with-asp-net-mvc.aspx

it seems like method 3 may be what I need, I'll try it when I get home.

optician
#3 is approx 10 times slower than the non-lambda versions and broken if you use ActionNameAttribute at all.
Craig Stuntz
+1  A: 

Just get rid of the slash and specify the controller and action explicitly:

<%= Html.ActionLink("Menu Items", "Item", "menuitems", 
    new RouteValueDictionary { { "id", item.id } })%>

You don't give an action name in your examples, so I guessed "Item." Insert the correct action name, obviously.

The current controller name is used if you use one of the ActionLink overloads which don't take a controller name.

I've written an in-depth explanation of routing, ActionLink, and more.

Craig Stuntz
This didn't work at first, as it added a length property, but this post helped me out. http://stackoverflow.com/questions/824279/why-does-html-actionlink-render-length4It would be good if someone could give me direction on how to add the id bit of the string, becuase my method feels bodgy <%= Html.ActionLink("Menu items", "menuitems/" + item.id, new { controller = "menuitems" }, new { id = item.id })%>It seems the last part of the action link may be redundant now, but it didn't do anything before
optician
The problem is that there are about 10 overloads of ActionLink, too many of which have params of type object. I'll change the syntax in my reply to a more robust version.
Craig Stuntz