MVC之-- ViewData vs ViewBag vs TempData vs Session
来源:互联网 发布:东南大学网络 编辑:程序博客网 时间:2024/05/21 14:48
In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Like WebForm, you can also use Session to persist data during a user session. Now question is that when to use ViewData, VieBag, TempData and Session. Each of them has its own importance. In this article, I am trying to explain the differences among these four.
ViewData
ViewData is a dictionary object that is derived from ViewDataDictionary class.
- public ViewDataDictionary ViewData { get; set; }
ViewData is a property of ControllerBase class.
ViewData is used to pass data from controller to corresponding view.
It’s life lies only during the current request.
If redirection occurs then it’s value becomes null.
It’s required typecasting for getting data and check for null values to avoid error.
ViewBag
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
- public Object ViewBag { get; }
ViewBag is a property of ControllerBase class.
It’s life also lies only during the current request.
If redirection occurs then it’s value becomes null.
It doesn’t required typecasting for getting data.
TempData
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
- public TempDataDictionary TempData { get; set; }
TempData is a property of ControllerBase class.
TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
It’s life is very short and lies only till the target view is fully loaded.
It’s required typecasting for getting data and check for null values to avoid error.
It is used to store only one time messages like error messages, validation messages. To persist data with TempData refer this article:Persisting Data with TempData
Session
Session is an object that is derived from HttpSessionState class.
- public HttpSessionState Session { get; }
Session is a property of HttpContext class.
Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it never expires.
Session is valid for all requests, not for a single redirect.
It’s also required typecasting for getting data and check for null values to avoid error.
Summary
In this article I try to explain the difference between ViewData, ViewBag and TempData. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
- MVC之-- ViewData vs ViewBag vs TempData vs Session
- ASP.net MVC+ViewData VS ViewBag
- .NET MVC TempData、ViewData、ViewBag
- ViewModel、ViewData、ViewBag、TempData、Session In MVC 對比
- ViewData vs ViewBag in ASP.NET MVC4
- MVC传递Model之TempData、ViewData、ViewBag区别及用途
- MVC中的ViewData、ViewBag和TempData
- 《MVC》——ViewData、ViewBag、TempData、model
- MVC中的ViewData、ViewBag和TempData
- tempdata,viewdata,viewbag
- ViewData/TempData/ViewBag 差異性
- TempData ViewBag ViewData
- ViewData ViewBag TempData
- MVC中ViewBag、ViewData和TempData 的区别与联系
- MVC中 ViewData、ViewBag、TempData的区别与联系
- ASP.Net MVC中 ViewBag, ViewData, TempData, ViewModel 的区别
- Asp.net MVC中ViewData、ViewBag和TempData区别
- MVC 的ViewBag, ViewData and TempData使用与区别
- RadioStream应用源码完整版(iphone版和ipad版)
- 使用ajaxFileUpload实现文件异步上传
- jsp页面引入的标签库
- 【已解决】【奇葩5】iOS 定位到香港
- 【水题】 Codeforces Round #289 509A Maximum in Table
- MVC之-- ViewData vs ViewBag vs TempData vs Session
- live555源代码分析与代码下载链接
- 最快最简单的排序——桶排序
- 更简约的用户账号设计:只需要本人实际姓名、E-mail和手机号码任选其一,无需密码
- 前一段时间没事用c#写了一个串口程序,这里总结一下自己的经验
- 使用 DrMemory 发现内存编程错误
- mysql安装
- MAP文件和调试(VC)(从崩溃地址找出错源码位置)
- git push时出现error: RPC failed; result=22, HTTP code = 411的问题