How to return View with QueryString in ASP.NET MVC 2?

家住魔仙堡 提交于 2019-12-07 00:52:44

问题


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?


回答1:


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:


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" });



回答3:


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.



来源:https://stackoverflow.com/questions/4344377/how-to-return-view-with-querystring-in-asp-net-mvc-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!