Introduction
Today, I am explaining about TempData vs ViewData vs Viewbag while working in MVC. Each one of the three (TempData, ViewData, ViewBag) terms are used to pass data from controller to View. These all have differing capacity to shield data while going from Controller to View using MVC application. In my recent articles, Explained thoroughly How to send SMS using C# and How to create charts using MVC.ViewBag
- ViewBag is used logically to pass the data from controller to view.
- It is being introduced since C# 4.0 .Net Framework.
- ViewData is property of ControllerBase Class from .Net Framework 4.5 C# first time.
- ViewBag life reach is short. It just lies under current requesting.
- If it redirects then its value becomes null.
- It doesn't required to typecasting for getting data from Controller to View.
- ViewBag is slower than ViewData.
//Controller Code snippet public ActionResult Index() { ViewBag.Tech = "Welcome to Technology Crowds"; return View(); }
Razor View Code Snippet
@ViewBag.Tech
ViewData
- ViewData is a property of ControllerBase Class.
- ViewData is derived from ViewDataDictionary Class.
- ViewData is used to pass data from Controller to View.
- It preserves the data only current request.
- Its value becomes null, if it redirects again.
- It is required strictly to typecast to avoid exception of null values.
- ViewData is faster than ViewBag.
public ViewDataDictionary ViewData { get; set; }
//Controller Code snippet public ActionResult Index() { ViewData["Tech"] = "Welcome to Technology Crowds"; return View(); } // Page View Code Snippet @ViewData["Tech"]
TempData
- TempData is a property of ControllerBase Class.
- ViewData is derived from TempDataDictionary Class.
- TempData is main purpose to preserve data from one request to subsequent requests while passing data from one page to another page.
- It is also required typecasting same as ViewData to avoid null exception.
public TempDataDictionary TempData { get; set; }
//Controller Code snippet public ActionResult TempData_Sample() { TempData["tc_msg"] = "Temp Data Sample..."; return RedirectToAction("TempData_SampleView", "TempViewVsViewBagvsViewData"); } public ActionResult TempData_SampleView() { return View(); } // Razor View Code Snippet@TempData["tc_msg"]
Summary
I have cleared up about TempData vs ViewData vs ViewBag in complete significance, If I get any proposal from gathering of spectators then for the most part welcome. I think these three terms TempData vs ViewData vs ViewBag is all around endeavored to keep up state in MVC. Each one of the three can use as indicated by our asking for situation. These work immensely while we controlling data or running beginning with one controller then onto the following or View so these fulfils our essentials, in case we understand these absolutely, we'll not face any issue to control data between Controllers and Views in our MVC applications. If any idea with respect to these I'll recognize from my heart.
Thanks for sharing this info...
ReplyDeleteWelcome, keep visiting here for latest MVC tutorials.
Delete