MVC Interview Question - 3

What is ViewData and View Bag?
ViewData, ViewBag is use for passing data from controller to
view and in next request. 

Viewdata contains the key, value pairs as dictionary and this is derived from class : "ViewDataDictionary". In action method we are setting the value for viewdata and in view the value will be fetched by typecasting.

ViewData and ViewBag are almost similar.

Similarity between ViewData and View Bag
1. Helps to maintain data when you move from controller to view.
2. Used to pass data from controller to corresponding view.

3. Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.

Difference between ViewBag & ViewData:

ViewData is a dictionary of objects that is derived from ViewDataDictionary class and is accessible using strings as keys.
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
ViewData requires typecasting for complex data type and check for null values to avoid error.
ViewBag doesn’t require typecasting for complex data type.

ViewBag & ViewData Example:

public ActionResult Index()


    ViewBag.Name = "Monjurul Habib";

    return View();


public ActionResult Index()


    ViewData["Name"] = "Monjurul Habib";

    return View();

In View:



What is the TempData

TempData is use for Passing data from Controller to View in ASP.NET MVC.
TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary.

TempData stays for a subsequent HTTP Request as opposed to other options (ViewBag and ViewData) those stay only for current request. So, TempdData can be used to maintain data between controller actions as well as redirects.

Below is sample example of TempData.

 //Controller Action 1 (TemporaryEmployee)

 public ActionResult TemporaryEmployee()


                Employee employee = new Employee


                        EmpID = "121",

                        EmpFirstName = "Imran",

                        EmpLastName = "Ghani"


                TempData["Employee"] = employee;

                return RedirectToAction("PermanentEmployee");


 //Controller Action 2(PermanentEmployee)

 public ActionResult PermanentEmployee()


               Employee employee = TempData["Employee"] as Employee;

               return View(employee);


As in above example, we store an employee object in TempData in Controller Action 1 (i.e. TemporaryEmployee) and retrieve it in another Controller Action 2 (i.e. PermanentEmployee). But If we try to do the same using ViewBag or ViewData, we will get null in Controller Action 2 because only TempData object maintains data between controller actions. An important thing about TempData is that it stores contents in Session object

What is the REST ?

Rest is Representational State Transfer.
REST is an architectural style which uses HTTP protocol 
methods like GET, POST, PUT, and DELETE to access the data.
ASP.Net MVC works in this style. 
In ASP.Net MVC 4 there is a support for Web API which uses 

to build the service using HTTP verbs.

MVC Interview Question                                  MVC Interview Question - 4

Posted in Labels: , |


Post a Comment