tags:

views:

148

answers:

2

What I'd like is for all urls to contain a certain parameter without having to pass it to the views and add it to ActionLink through the routevalues param. I have a section in my site that I want to keep track of a "return param" for all links. It works fine for forms since Html.BeginForm sets the action to the exact current url already.

So, if the page I'm at is

/MyController/MyAction/300?ReturnTo=100

and I output

Html.ActionLink("Next Screen", "MyOtherAction")

I'd like to see

<a href="/MyController/MyOtherAction/300?ReturnTo=100">Next Screen</a>

without having to do

Html.ActionLink("Next Screen", "MyOtherAction", new {ReturnTo = Model.ReturnTo})
+2  A: 

You could write your own HtmlHelper extension that does this by calling Html.ActionLink, and combining the query strings. Use the ViewContext to get at the current query string.

Lance Fisher
That sounds pretty good. Does it strike you as breaking mvc conventions to access HttpContext.Current.Request in a view helper?
jayrdub
You don't need to, exactly. It is available via the helper argument.
Craig Stuntz
A: 

Try this:

Html.ActionLink("Next Screen", "MyOtherAction", new {Model.ReturnTo})

The link will now appear the way you want it to be.

You've got to read the whole question before answering, the line you wrote is the exact line I was wondering how not to write.
jayrdub