Asp.net-Mvc-2

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

  • August 10, 2017

我正在用 ASP.NET MVC 2 開發一個網站。在某些時候,我在控制器中得到一個 ActionResult,我顯然呼叫了方法

return View();  

有什麼方法可以將 QueryString 傳遞到我的視圖中或將參數附加到 URL?

你可以試試

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

不要忘記包括這個

using System.Web.Routing;

或者你可以試試這個

return RedirectToAction("Index?ParamID=1234");

視圖應該操縱控制器傳遞的模型。向相應操作發出請求時,查詢字元串參數已經存在。所以要傳遞一個視圖模型:

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

現在在您看來,您可以使用此模型。

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

return RedirectToAction("SomeOtherActionName", new { ParamName = "ParamValue" });

引用自:https://stackoverflow.com/questions/4344377