重定向之前设置Viewbag [英] Set Viewbag before Redirect

查看:204
本文介绍了重定向之前设置Viewbag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能设置ViewBag我叫重定向过吗?

我想是这样

  @ ViewBag.Message =MyMessage;
RedirectToAction(MyAction);


解决方案

当你使用重定向,不得使用 ViewBag ,但的TempData

 公众的ActionResult措施1 {
 TempData的[的ShortMessage] =MyMessage;
 返回RedirectToAction(措施2);
}公众的ActionResult措施2 {
 //现在我可以填充我ViewBag(如果我愿意的话)与TempData的[的ShortMessage]的内容
  。ViewBag.Message = TempData的[的ShortMessage]的ToString();
  返回查看();
}

Is it possible to set the ViewBag before i call a redirection?

I want something like

@ViewBag.Message="MyMessage";
RedirectToAction("MyAction");

解决方案

When you use redirection, you shall not use ViewBag, but TempData

public ActionResult Action1 {
 TempData["shortMessage"] = "MyMessage";
 return RedirectToAction("Action2");
}

public ActionResult Action2 {
 //now I can populate my ViewBag (if I want to) with the TempData["shortMessage"] content
  ViewBag.Message = TempData["shortMessage"].ToString();
  return View();
}

这篇关于重定向之前设置Viewbag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆