Thursday, 11 December 2014

ViewData, ViewBag and TempData in .Net MVC

~Difference Between ViewData, ViewBag and TempData~


Introduction
In Asp.Net MVC there are three ways to pass/store data between the controllers and views.

ViewData
  1. ViewData is used to pass data from controller to view
  2. It is derived from ViewDataDictionary class
  3. It is available for the current request only
  4. Requires typecasting for complex data type and checks for null values to avoid error
  5. If redirection occurs, then its value becomes null
 In Controller:  public ActionResult Index()
              {
               ViewData["Name"] = "Monjurul Habib";
                return View();
              }
  In View:             @ViewData["Name"]

ViewBag
  1. ViewBag is also used to pass data from the controller to the respective view
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
  3. It is also available for the current request only
  4. If redirection occurs, then its value becomes null
  5. Doesn’t require typecasting for complex data type

 In Controller:  public ActionResult Index()
              {
               ViewBag.Name = "Monjurul Habib";
                return View();
              }
  In View:             @ViewBag.Name

TempData
  1. TempData is derived from TempDataDictionary class
  2. TempData is used to pass data from the current request to the next request
  3. It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
  4. It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages.
public ActionResult Index()
{
  var model = new Review()
            {
                Body = "Start",
                Rating=5
            };
    TempData["ModelName"] = model;
    return RedirectToAction("About");
}

<pre><pre lang="cs">
public ActionResult About()
{
    var model= TempData["ModelName"];
    return View(model);
}

Related Posts:

  • Login With Google Here in This Article I am going to explain you how to implement Login with Google in your website. first of all you need to create a new pr… Read More
  • Login with facebook Here in This Article I am going to explain you how to implement Login with facebook in your website. first of all you need to create a new … Read More
  • what is DTO what is Data Transfer Object? A Data Transfer Object is an object that is used to encapsulate data, and send it from one subsystem of an applica… Read More
  • MVC3 - Action method Calling with JSON and AJAX this Article describes how to Call a Action Method In Controller with JSON or AJAX. there are different ways to Call the Action .you can call action … Read More
  • Calling WebApi from MVC layer In this Project you will be able to learn how you can call Web Api functions from MVC Layer project. Follow the Steps: 1- First Create a new MVC4 … Read More

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives