Everything you need to know about ViewBag and ViewData in Asp.Net Core MVC
By: Nitish Kaushik
Tags: Technology, Development, Programming, Aspnetcore, Tutorial

What is ViewBag and ViewData in Asp.Net Core?

In simple terms:

ViewBag and ViewData both are properties available in Microsoft.AspNetCore.Mvc namespace. ViewBag returns dynamic data type while ViewData returns ViewDataDictionary type.

Why do we use ViewBag and ViewData in Asp.Net Core?

ViewBag and ViewData both are used to pass data from the controller’s action method to its corresponding view or associated layout page to the view.

ViewBag and ViewData are used to:

Pass data from the action method to related view

Pass data from the action method to the associated layout

Pass data from view to its layout file

partial view to the view

This way of passing the data is called as weakly data binding.

Let us understand these concepts in detail.

# ViewBag in Asp.Net Core:

ViewBag is a getter property available in Microsoft.AspNetCore.Mvc namespace. ViewBag is available in all controllers of the Asp.Net Core application. ViewBag works with dynamic it means its value is resolved at runtime.

How to assign value to ViewBag:

We generally assign value either in a controller or in the view file. In the controller, we can use ViewBag directly but on the view file, we need to use razor syntax.

To assign the data to a ViewBag we need to create a dynamic property. This dynamic property can be added as below:

ViewBag.PropertyName

Now, we have the ViewBag ready, we just need to assign its value. And the value can be assigned using the following:

ViewBag.PropertyName = Value;

In case you are assigning a value to ViewBag in view (cshtml) file, then you need to use razor syntax.

@{ ViewBag.PropertyName = Value; }

The type of value can be anything for example — number, string, collection, object, anonymous object, etc.

Click here to know more about working with an anonymous object in ViewBag.

How to get value from ViewBag:

Getting the value from ViewBag is very simple on a cshtml page. We can get ViewBag value using razor syntax.

We can use ViewBag value directly on view page or we can assign its value to some other variable.

· If we want to use ViewBag value directly on the view page —

@ViewBag.PropertyName

If we want to need to assign ViewBag value to some other variable on view page –

@{

var variableName = ViewBag.PropertyName

}

Important points to remember while working with ViewBag

We can use n numbers (multiple) of ViewBag in a single action method or view page

Value is resolved at runtime it means we won’t get any compile-time error

Since its dynamic hence we will not get any IntelliSense

If the ViewBag contains an object, then its value can be accessed using

ViewBag.PropertyName.ObjctPropertyName

If the ViewBag contains a collection of data, then the iteration can be applied directly.

Foreach loop of ViewBag

# ViewData in asp.net core

ViewData is a getter and setter property available in Microsoft.AspNetCore.Mvc namespace. ViewData is also available in all controllers of the Asp.Net Core application. ViewData works with ViewDataDictionary. This ViewDataDictinonary works on the key-value concept.

The key is in the form of a string and the value is in the form of an object .

How to assign value to ViewData:

We generally assign value to ViewData either in a controller or in the view file. In the controller, we can use ViewData directly but on the view file, we need to use razor syntax.

Since ViewDataDictionary works on the Key and Value concept it means the key of ViewData should be a valid string.

ViewData[“PropertyName”]

Now, we have the ViewData ready, we just need to assign its value. And the value is of object type it means we can assign any type directly using the following syntax -

ViewData[“PropertyName”] = Value;

In case you are assigning a value to ViewData in view (cshtml) file, then you need to use razor syntax.

@{ ViewData[“PropertyName”] = Value; }

The type of value can be anything for example – number, string, collection, object, anonymous object, etc.

How to get value from ViewData:

Getting the value from ViewData is very simple and is possible using razor syntax.

We can use ViewDaa value directly on view page or we can assign its value to some other variable. Remember the ViewData contains value in the form of object hence it needs to be cast to the specific type before using it.

If we want to use ViewData value directly on the view page

@ViewData[“PropertyName”]

If we want to need to assign ViewData value to some other variable on the view page

@{

var variableName = ViewData[“PropertyName”]

}

Example: Suppose we are passing a list of integers to ViewData and we want to use on view file then

Assign list to ViewData:

Assign data to ViewData

And now let’s iterate its value on view file:

For each loop on ViewData on view file

Important points to remember while working with ViewData