9

I'm developing a web site in ASP.NET MVC 2. At some point, I get to a ActionResult in a controller and I obviously call method

return View();  

Is there any way, that I could pass QueryString into my view or attach parameters to the URL?

dani
  • 469
  • 3
  • 12
  • 23

3 Answers3

12

A view is supposed to manipulate the model which is passed by the controller. The query string parameters are already present when the request was made to the corresponding action. So to pass a view model:

var model = new MyViewModel
{
    SomeParam = "Some value"
}
return View(model);

And now in your view you could use this model.

If on the other hand you don't want to return a view but redirect to some other controller action you could:

return RedirectToAction("SomeOtherActionName", new { ParamName = "ParamValue" });
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
4

You can try

public ActionResult Index()
{
    RouteValueDictionary rvd = new RouteValueDictionary();
    rvd.Add("ParamID", "123");
    return RedirectToAction("Index", "ControllerName",rvd);
}

Don't forget to include this

using System.Web.Routing;

or simply you can try this

return RedirectToAction("Index?ParamID=1234");
Carls Jr.
  • 3,088
  • 7
  • 37
  • 57
1

For me, I was losing the query string on a form POST. Request.QueryString was empty in the controller post action.

So, what I did was include the query string in the form action.

There are several ways to do this. The answers are listed here:

Using Html.BeginForm with querystring

Sorry for the link only answer, but I don't want to duplicate the work of these answers here. Besides I hope it is helpful to someone to realize that you could be losing the query string by a form post.

Jess
  • 23,901
  • 21
  • 124
  • 145