方法一可以将您想要的参数作为 RedirectToAction() 方法的 routeValues 参数的一部分传递。使用传递的查询字符串数据。
或者您可以借助以下查询字符串对其进行框架化:
return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "YourActionName", Id = Id}) );
或者您可以使用 TempData:
[HttpPost]
public ActionResult MyActionMethod(MyModel model)
{
TempData["myModal"]= new MyModel();
return RedirectToAction("ActionMethod2");
}
[HttpGet]
public ActionResult ActionMethod2()
{
MyModel myModal=(MyModel)TempData["myModal"];
return View();
}
在浏览器的 URL 栏中。
此解决方案使用临时 cookie:
[HttpPost]
public ActionResult Settings(SettingsViewModel view)
{
if (ModelState.IsValid)
{
//save
Response.SetCookie(new HttpCookie("SettingsSaveSuccess", ""));
return RedirectToAction("Settings");
}
else
{
return View(view);
}
}
[HttpGet]
public ActionResult Settings()
{
var view = new SettingsViewModel();
//fetch from db and do your mapping
bool saveSuccess = false;
if (Request.Cookies["SettingsSaveSuccess"] != null)
{
Response.SetCookie(new HttpCookie("SettingsSaveSuccess", "") { Expires = DateTime.Now.AddDays(-1) });
saveSuccess = true;
}
view.SaveSuccess = saveSuccess;
return View(view);
}
或尝试方法 4:
只需调用操作,无需重定向到操作或模型的新关键字。
[HttpPost]
public ActionResult MyActionMethod(MyModel myModel1)
{
return ActionMethod2(myModel1); //this will also work
}
public ActionResult ActionMethod2(MyModel myModel)
{
return View(myModel);
}