1

Is it possible to attach a parameter to every single @Html.ActionLink without changing the code in the view file?

Something like:

public ActionResult Edit(int id)
{
    RouteData.Values.Add("param_name", "param_value"); // This does not work
    return View();
}

I want all the links to contain the parameter:

<a href="/controller/action/1?param_name=param_value">test</a>

Session is not a good option because users can open multiple windows in the browser.

Corneliu
  • 2,932
  • 1
  • 19
  • 22

2 Answers2

0

Why do you want to do this in the controller? With @Html.ActionLink its as simple as:

@Html.ActionLink("Edit", "Edit", "Controller", 
                 new { id = 1, param_name = "param_value" })

This should generate:

<a href="/Controller/Edit/1?param_name=param_value">Edit</a>

If for some reason this is not applicable to your project you should be able to do this with routing.

shuniar
  • 2,592
  • 6
  • 31
  • 38
  • I want to attach this parameter to all links on the page, including links defined on layout page, partial views etc. – Corneliu Oct 20 '11 at 20:09
  • @Corneliu you may want to try creating your own route handler like [this post](http://stackoverflow.com/questions/7708155/how-to-replace-with-in-url-before-mapping-a-controller-and-an-action-in) suggests. – shuniar Oct 21 '11 at 00:13
0

It is possible on the route level, see ASP.NET MVC: The right way to propagate query parameter through all ActionLinks. If you want to do it on Action/View level, then you can probably still make a route class that will generate URLs based on some data set by controller.

Community
  • 1
  • 1
Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162