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?

3 Answers 3

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" });
Sign up to request clarification or add additional context in comments.

1 Comment

how can I also pass a model, as well as a query string?
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");

2 Comments

Call me crazy but this looks like an infinite loop.
have you found any solution for your problem. I am trying to do the same thing. When i return view(model) it removes url parameter.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.