viewbag viewdata tempdata 区别以及跨controller 传值
来源:互联网 发布:uitableview优化 简书 编辑:程序博客网 时间:2024/05/02 01:47
问题:
TempData
ViewBag
ViewData
I have a requirement, where I need to set a value in a controller one, that controller will redirect to Controller Two and Controller Two will render the View.
I have tried to use ViewBag, the value gets lost by the time I reach Controller Two.
Can I know when to use and advantages or disadvantages?
回答:
1)TempData
Allows you to store data that will survive for a redirect. Internally it uses the Session as baking store, it's just that after the redirect is made the data is automatically evicted. The pattern is the following:
public ActionResult Foo(){ // store something into the tempdata that will be available during a single redirect TempData["foo"] = "bar"; // you should always redirect if you store something into TempData to // a controller action that will consume this data return RedirectToAction("bar");}public ActionResult Bar(){ var foo = TempData["foo"]; ...}
2)ViewBag, ViewData
Allows you to store data in a controller action that will be used in the corresponding view. This assumes that the action returns a view and doesn't redirect. Lives only during the current request.
The pattern is the following:
public ActionResult Foo(){ ViewBag.Foo = "bar"; return View();}
and in the view:
@ViewBag.Foo
or with ViewData:
public ActionResult Foo(){ ViewData["Foo"] = "bar"; return View();}
and in the view:
@ViewData["Foo"]
ViewBag
is just a dynamic wrapper around ViewData
and exists only in ASP.NET MVC 3.
This being said, none of those two constructs should ever be used. You should use view models and strongly typed views. So the correct pattern is the following:
View model:
Action:
public Action Foo(){ var model = new MyViewModel { Foo = "bar" }; return View(model);}
Strongly typed view:
@model MyViewModel@Model.Foo
After this brief introduction let's answer your question:
My requirement is I want to set a value in a controller one, that controller will redirect to ControllerTwo and Controller2 will render the View.
public class OneController: Controller{ public ActionResult Index() { TempData["foo"] = "bar"; return RedirectToAction("index", "two"); }}public class TwoController: Controller{ public ActionResult Index() { var model = new MyViewModel { Foo = TempData["foo"] as string }; return View(model); }}
and the corresponding view (~/Views/Two/Index.cshtml
):
@model MyViewModel@Html.DisplayFor(x => x.Foo)
There are drawbacks of using TempData as well: if the user hits F5 on the target page the data will be lost.
Personally I don't use TempData neither. It's because internally it uses Session and I disable session in my applications. I prefer a more RESTful way to achieve this. Which is: in the first controller action that performs the redirect store the object in your data store and user the generated unique id when redirecting. Then on the target action use this id to fetch back the initially stored object:
public class OneController: Controller{ public ActionResult Index() { var id = Repository.SaveData("foo"); return RedirectToAction("index", "two", new { id = id }); }}public class TwoController: Controller{ public ActionResult Index(string id) { var model = new MyViewModel { Foo = Repository.GetData(id) }; return View(model); }}
The view stays the same.
转地址:http://stackoverflow.com/questions/7993263/viewbag-viewdata-and-tempdata
- viewbag viewdata tempdata 区别以及跨controller 传值
- tempdata,viewdata,viewbag
- ViewData/TempData/ViewBag 差異性
- TempData ViewBag ViewData
- ViewData ViewBag TempData
- MVC中ViewBag、ViewData和TempData 的区别与联系
- MVC中 ViewData、ViewBag、TempData的区别与联系
- MVC3中 ViewBag、ViewData和TempData的使用和区别
- ASP.Net MVC中 ViewBag, ViewData, TempData, ViewModel 的区别
- Asp.net MVC中ViewData、ViewBag和TempData区别
- MVC 的ViewBag, ViewData and TempData使用与区别
- MVC3中 ViewBag、ViewData和TempData的使用和区别
- MVC中的ViewData与ViewBag与TempData的区别
- MVC3中 ViewBag、ViewData和TempData的使用和区别
- MVC传递Model之TempData、ViewData、ViewBag区别及用途
- MVC3中 ViewBag、ViewData和TempData的使用和区别
- MVC中ViewBag、ViewData和TempData 的区别与联系
- MVC中TempData、ViewData和ViewBag的区别
- hdu 5163 前缀和+枚举
- exec 重定向
- js自带弹窗总结(alert,confirm,prompt)
- Linux 用户和用户组 配置说明
- 加速度传感器测试APP
- viewbag viewdata tempdata 区别以及跨controller 传值
- 改变系统状态栏颜色(仅4.4或以上有效)
- Python读写csv文件
- java单例模式
- centos7查看ssh
- js乘除法
- 1034. Head of a Gang (30)
- Android:实现最简单的单指移动、双指缩放的图片组件
- 解决iText 5.X以上,加入iTextAsian.jar 出现异常 Font 'STSong-Light' with 'UniGB-UCS2-H找不到的'的异常