ASP.Net MVC: ViewData, ViewBag and TempData with examples

In this section, I will explain the purpose of ViewData, ViewBag, and TempData with examples.

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 Cast 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.

Syntax:-

C# Code:


 public class Parent1Controller : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to Dotnet Builders Tutorials";
            return View();
        }
    }

.CSHTML CODE:


<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <center>
        <h2>@ViewData["Message"]</h2>
    </center>
</body>
</html>

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.


Syntax:-

C# Code:

 public class Parent1Controller : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to Dotnet Builders Tutorials";
            return View();
        }
    }

.CSHTML CODE:

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <center>
        <h2>@ViewBag.Message</h2>
    </center>
</body>
</html>

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 Cast 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.


Syntax:-

C# Code:

Controller1:
 public class Parent1Controller : Controller
    {
        public ActionResult Index()
        {
TempData["Message"] = "Welcome to Dotnet Builders Tutorials";
       return RedirectToAction("Index","Child1");
} }

Controller2:
public class Child1Controller : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }

.CSHTML CODE:

Child View:

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <center>
        <h2>@TempData["Message"]</h2>
    </center>
</body>
</html>


Thank You
I wish you liked this, I'll come with some other exciting parts of MVC operations soon until then enjoy coding and if you encounter any problem then feel free to ping me.

Comments

Post a Comment

Popular posts from this blog

Send Mail with Attachment using MVC

Difference Between Web Services, WCF and WEB API