方法一可以传递您想要的参数作为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();
}
在浏览器的地址栏。
该解决方案使用临时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);
}
我不想在任何视图中使用ActionMethod2()的结果,只是我想用它作为变量值。 – Elnaz
请注意,模型 - 视图 - 控制器标签是针对有关该模式的问题。 ASP.NET-MVC实现有一个特定的标签。 –