Saturday, 9 June 2018

ViewData vs ViewBag vs TempData

ViewData, ViewBag and TempData in ASP.Net MVC. Let's see the differences among these using code implementation.

What is ViewData?


ViewData in MVC is used to transfer data from Controller to View.
Every controller has a property called as 'ViewData' of type ViewDataDictionary, which contain key-value pair.

A simple way to assign value to ViewData.



public ActionResult DisplayProductName()
        {
            ViewData["ProdName"] = "Laptop"
            return View();
        }

Access ViewData in View as shown below.


@ViewData["ProdName"]


Another Example of ViewData with sample code:

A Product model class.


public class Products
    {
        public int ProductID { get; set; }
        public double ProductCost { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
    }


A controller with ActionResult Index


public ActionResult Index()
        {
            var prd = new Products() { ProductID = 1, ProductName = "MVC Ebook", ProductCost = 500, ProductDescription = "An Ebook for MVC" };
            ViewData["Product"] = prd;
            return View();
        }


View index.cshtml


<div>
    @{
        var products =  (MVCInterview.Models.Products)ViewData["Product"];
       
    }
    <ul>
        <li>ID:@products.ProductID</li>
        <li>ID:@products.ProductName</li>
        <li>ID:@products.ProductCost</li>
        <li>ID:@products.ProductDescription</li>

    </ul>

</div>


Output -



What is ViewBag?


It is similar to ViewData, but the difference is instead of using a string value, ViewBag uses dynamic type.ViewBag was introduced in ASP.Net MVC 3.0;
A sample code to assign value to ViewBag.


public ActionResult DisplayProductName()
        {
            var product = new Products { ProductName = "Laptop" };
            ViewBag.Product = product;
            return View();
        }

Access ViewBag in View -



@ViewBag.Product.ProductName

What is TempData?


TempData is useful, if you want to transfer data from one action to another action within same controller or different controller.

TempData in MVC is used to store temporary data for a short period of time.
What is the duration of short period time?
Here, short period of time means for a subsequent request. Data stored in TempData will be lost after completion of subsequent request.

Once you request - localhost/Test/Book then it will redirec to localhost/Test/DisplayBook  and display the result - "MVC Ebook".



public class TestController : Controller
    {
        public ActionResult Book()
        {
            TempData["product"] = "MVC Ebook";
            return RedirectToAction("DisplayBook");
        }

        public string DisplayBook()
        {
            return TempData["product"].ToString();
        }

    }


But when you refresh the page again it will throw error, see below screen shot.




Summary -

ViewBag is used to set and get value dynamically. ViewBag uses dynamic property to store data.
ViewData is also use to store data similar to ViewBag. ViewData is of type ViewDataDictionary.
We can access ViewBag and ViewData in corresponding View.
TempData is used to pass data from one action to another action, Action could be from same controller or different controller.

Previous blog on MVC-


No comments:

Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks