Similarities Between ViewData And ViewBag In ASP.Net MVC

  • Both ViewData and ViewBag are used to pass data from a controller to a view.
  • Both Helps to maintain data when you move from controller to view.
  • Short life means value becomes null when redirection occurs.
  • ViewData and ViewBag are DataDictionary objects.
  • Both ViewData and ViewBag does not provide compile time error checking. For Example– if you mis-spell keys you wouldn’t get any compile time errors. You get to know about the error only at runtime.

Differences Between ViewData And ViewBag In ASP.Net MVC

VIEW DATAVIEW BAG
ViewData is a dictionary of objects that is derived from ViewDataDictionary class and is accessible using strings as keysViewBag uses dynamic feature that was introduced in C# 4.0. It allows an object to have properties dynamically added to it.
ViewData requires typecasting when you use complex data type to avoid error.ViewBag does not require typecasting when you use complex data type.
ViewData[“<Key>”] = <Value>;ViewBag.<PropertyName> = <Value>;

Note: To pass data from controller to a view, it’s always a good practice to use strongly typed view models over ViewBag and ViewData. Strongly typed view models provide compile time error checking.

Source Code Of HomeController

using Difference_ViewBag_Viewdata.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Difference_ViewBag_Viewdata.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            ViewData["Message1"] = "Data comes form ViewData";
            ViewBag.Message2 = "Data comes form ViewBag";

            ViewData["CurrentDate1"] = DateTime.Now.ToString();
            ViewBag.CurrentDate2 = DateTime.Now.ToString();

            string[] games = {"Hockey","Football","Cricket","Baseball" };

            ViewData["GamesArray1"] = games;
            ViewBag.GamesArray2 = games;

            Employee Asim = new Employee();
            Asim.Empid = 11;
            Asim.EmpName = "Asim Khan";
            Asim.EmpAge = 25;

            ViewData["Employee1"] = Asim;
            ViewBag.Employee2 = Asim;



            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

Source Code Of Index.cshtml – View


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<h3>@ViewData["Message1"]</h3>
<h3>@ViewBag.Message2</h3>

<h3>@ViewData["CurrentDate1"]</h3>
<h3>@ViewBag.CurrentDate2</h3>

<ul>
    @{
        foreach (string i in (string[])ViewData["GamesArray1"])
        {
            <li>@i</li>
        }
    }
</ul>

<ul>
    @{
        foreach (string i in ViewBag.GamesArray2)
        {
            <li>@i</li>
        }
    }
</ul>

@{ 
    var a = (Difference_ViewBag_Viewdata.Models.Employee)ViewData["Employee1"];
    var b = ViewBag.Employee2;
}

@{ 
    <h3>@a.Empid</h3>
    <h3>@a.EmpName</h3>
    <h3>@a.EmpAge</h3>
}

@{ 
    <h3>@b.Empid</h3>
    <h3>@b.EmpName</h3>
    <h3>@b.EmpAge</h3>
}

Source Code Of About.cshtml – View


@{
    ViewBag.Title = "About";
}

<h2>About</h2>

<h3>ViewData data is = @ViewData["Message1"]</h3>
<h3>ViewBag data is = @ViewBag.Message2</h3>

Click Below Link to Download Notes Of This Blog

https://www.mediafire.com/file/9hj08pvffxpbxbl/SIMILARITIES+AND+DIFFERENCES+BETWEEN+VIEW+DATA+AND+VIEW+BAG+IN+MVC.docx/file

Click Below Link to Download Source Code Project Of This Blog

https://www.mediafire.com/file/fwf5v6vjihhcdff/Difference_ViewBag_Viewdata.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *