Search code examples
model-view-controllerasp.net-mvc-2asp.net-mvc-routingasp.net-routing

ASP.NET MVC 2 Point Return Links to Appropriate View


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


Solution

  • 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.