C#

viewbag viewdata and tempdata in mvc


In this blog, I will explain with an example, the Similarities and Differences of ViewData, ViewBag and TempData in ASP.Net MVC.

ViewData and ViewBag are used for transferring data and objects between Controller to View and TempData  is used for transferring data from controller to view or one Controller to another controller in ASP.Net MVC.

ViewData:-

       1.      ViewData is derived from the ViewDataDictionary class and is basically a Dictionary   object           i.e. Keys and Values where Keys are String while Values will be objects.
       2.     Data is stored as Object in ViewData.
       3.     While retrieving, the data it needs to be Type Casted to its original type as the data is stored as             objects and it also requires NULL checks while retrieving.
       4.       ViewData is used for passing value from Controller to View.
       5.       ViewData is available only for Current Request. It will be destroyed on redirection.


Example
In the below example, a string value is set in the ViewData object in Controller and it is then displayed in View.
Controller

using System.Web.Mvc;

namespace Practice.Controllers
{
    public class DemoController : Controller
    {
        // GET: Demo
        public ActionResult Index()
        {
            ViewData["UserName"] = "Welcome Pawan Kumar";
            return View();
        }
    }
}

viewdata in mvc



View
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{
            string name = (string)ViewData["UserName"];
        }
        <div>@name</div>
    </div>
</body>
</html>

viewdata in mvc



ViewBag:-

      1.      ViewBag is a Wrapper built around ViewData.
      2.      ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
      3.      While retrieving, there is no need for Type Casting data.
      4.      ViewBag is used for passing value from Controller to View.
      5.      ViewBag is available only for Current Request. It will be destroyed on redirection.

Example
In the below example, a string value is set in the ViewBag object in Controller and it is then displayed in View.
Controller

using System.Web.Mvc;

namespace Practice.Controllers
{
    public class DemoController : Controller
    {
        // GET: Demo
        public ActionResult Index()
        {
            ViewBag.UserName = "Welcome Pawan Kumar";
            return View();
        }
    }
}

viewbag in  mvc



View


<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{
            string name = ViewBag.UserName;//no need of type casting
        }
        <div>@name</div>
    </div>
</body>
</html>

viewbag in  mvc



TempData:-

      1.      TempData is derived from the TempDataDictionary class and is basically a Dictionary object             i.e. Keys and Values where Keys are String while Values will be objects.
      2.      Data is stored as Object in TempData.
      3.      While retrieving, the data it needs to be Type Casted to its original type as the data is
      stored as objects and it also requires NULL checks while retrieving.
      4.      TempData can be used for passing value from Controller to View and also from Controller to              Controller.
      5.      TempData is available for Current and Subsequent Requests. It will not be destroyed on redirection.


Example
In the below example, a string value is set in the TempData object in Controller and it is redirected to another Controller and finally it is displayed in View.
First Controller

using System.Web.Mvc;

namespace Practice.Controllers
{
    public class DemoController : Controller
    {
        // GET: Demo
        public ActionResult Index()
        {
            TempData["UserName"] = "Welcome Pawan Kumar";
            return View();
        }
    }
}



tempdata in mvc


View


<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @{
            string name = (string)TempData["UserName"];//casting is required
        }
        <div>@name</div>
    </div>
</body>
</html>

tempdata in mvc

Example-Get Value of TempData from one Action Method to another ActionMethod


using System.Web.Mvc;

namespace Practice.Controllers
{
    public class DemoController : Controller
    {
        // GET: Demo
        public ActionResult Index()
        {
            //Assgin value in tempdata
            TempData["UserName"] = "Welcome Pawan Kumar";
            return RedirectToAction("TempDataDemo","Demo");
        }

        public ActionResult TempDataDemo()
        {
            //getting value in subsequent request
            string username = (string)TempData["UserName"];
            return View();
        }
    }
}
Note:-
ViewData and ViewBag are almost similar and it helps us to transfer the data from controller to view whereas TempData also works during the current and subsequent requests.


No comments:

Post a Comment