如何在ASP.NET MVC 2中使用QueryString返回View?

dan*_*ani 7 return view query-string asp.net-mvc-2

我正在ASP.NET MVC 2中开发一个网站.在某些时候,我在控制器中找到一个ActionResult,我显然是在调用方法

return View();  
Run Code Online (Sandbox Code Playgroud)

有什么办法,我可以将QueryString传递给我的视图或将参数附加到URL?

Dar*_*rov 9

视图应该操纵控制器传递的模型.请求对相应的操作时,查询字符串参数已存在.所以传递一个视图模型:

var model = new MyViewModel
{
    SomeParam = "Some value"
}
return View(model);
Run Code Online (Sandbox Code Playgroud)

现在在您的视图中,您可以使用此模型.

另一方面,如果您不想返回视图但重定向到其他控制器操作,您可以:

return RedirectToAction("SomeOtherActionName", new { ParamName = "ParamValue" });
Run Code Online (Sandbox Code Playgroud)

  • 我怎样才能传递模型以及查询字符串? (5认同)

Car*_*Jr. 4

你可以试试

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

不要忘记包括这个

using System.Web.Routing;
Run Code Online (Sandbox Code Playgroud)

或者你可以试试这个

return RedirectToAction("Index?ParamID=1234");
Run Code Online (Sandbox Code Playgroud)