views:

28

answers:

2

The default scaffolded views generated by ASP.NET MVC 2 contain links such as:

<%: Html.ActionLink("Back to List", "Index") %>
<%: Html.ActionLink("Create New", "Create") %>

These links are perfect if I came to this page from that same root. But for example, if I have Orders and Persons and I navigate to /Orders/Edit/17 via /Persons/Orders/3, then 'back to list' returns me to Orders root not Persons root, where I want to go, because the 'Edit Orders' view only knows about orders. This makes the navigation awkward and breaks the flow..

I want to reuse the same 'Edit Orders' view regardless of where I came from, but I'm not sure how to pass this information.

I know it's possible to pass parameters like /Orders/Edit/17?myparam=myvalue but will this limit my choices later on if I need to pass parameters which indicate Sort/Filter order for grids?

What is the preferred way to pass a return/origin location to my view so that it can render the links properly? Otherwise, how can I call the view differently from the controller?

EDIT:

For a clean solution, see THIS POST

+1  A: 

Passing in parameters through the querystring will not really limit you as long as you don't use the same names. There is a size restriction on querystrings, but you'll probably not hit it.

That's basically how I do it. I'm curious to see what others answer.

Dan at Demand
This functionality seems like something that should have been accounted for in the framework; it seems fairly hacky to have to specify these parameters because you'd have to do so for every level of navigation..
Harper
A: 

This functionality seems like something that should have been accounted for in the framework; it seems fairly hacky to have to specify these parameters because you'd have to do so for every level of navigation.

Why? back is something that belongs in browsers, along with history. Its been like that for a while, why should the framework need to handle that?

What you need its not the norm.

Additionally, you are responsible to keep your code dry. You can definitely handle it in a way that all the repetition you have is the name of the function you are calling.

eglasius
This doesn't have anything to do with the 'Back' function. I've edited my post with the correct solution; you should look at it - it might help you.
Harper
@Harper after re-reading your question I see that the reason you are duplicating browser functionality is because of the default scaffolded views / that's not a good argument to do it. About the link, you can do very similar with both RenderPartial and RenderAction, you could even do it with a full blown view. I thought you had look a bit more into it, since passing model data to the views is what you do in pretty much anything in it.
eglasius