In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article before proceeding to this article where we discussed ViewData in ASP.NET Core MVC application. As we already discussed in the ASP.NET Core MVC application, we can pass the data from a controller to a view using ViewData, ViewBag, and strongly typed view model. As part of this article, we are going to discuss the following pointers.
The ViewBag in ASP.NET Core MVC is one of the mechanisms to pass the data from a controller action method to a view. If you go to the Controller base class, then you will find the following signature of the ViewBag property.
So the ViewBag is a dynamic property of the Controller base class. The dynamic type is introduced in C# 4.0. It is very much similar to the var keyword that means we can store any type of value in it but the type will be decided at run time rather than compile-time.
The ViewBag transfers the data from the controller action method to a view only, the reverse is not possible.
How to Pass and Retrieve data From ViewBag in ASP.NET Core MVC?
The point that you need to keep in mind is, ViewBag is operating on the dynamic data type. So we don’t require typecasting while accessing the data from a ViewBag. It does not matter whether the data that we are accessing is of type string or any complex type.
ViewBag in ASP.NET Core MVC with String Type:
ViewBag in ASP.NET Core MVC with Complex Type:
Example of ViewBag in ASP.NET Core MVC:
Let us see an example to understand how to use ViewBag to pass data from a controller to a view. We are going to work with the same example that we worked in our previous article with ViewData. So, modify the Details action method of HomeController class as shown below.
Accessing the ViewBag in a View in ASP.NET Core MVC
Now we will see how to access the ViewBag data within an ASP.NET Core MVC view. So, modify the Details.cshtml view file as shown below.
The ViewBag is a dynamic property that is resolved at runtime; as a result, here also it will not provide compile-time error checking as well as intelligence support. For example, if we miss-spell the property names of the ViewBag, then we wouldn’t get any compile-time error rather we came to know about the error at runtime.
Difference between ViewData and ViewBag in ASP.NET Core MVC
In the next article, I am going to discuss the Strongly Typed Views in ASP.NET Core MVC application with an example. Here, in this article, I try to explain ViewBag in ASP.NET Core MVC application. I hope this article will help you with your needs.
Summary:
I hope this post will be helpful to understand the concept of ViewBag in ASP.NET Core MVC
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
- What is ViewBag in ASP.NET Core MVC?
- How to Pass and Retrieve data From ViewBag in ASP.NET Core MVC?
- Example to understand ViewBag in ASP.NET Core MVC
- Difference between ViewData and ViewBag in ASP.NET Core MVC
The ViewBag in ASP.NET Core MVC is one of the mechanisms to pass the data from a controller action method to a view. If you go to the Controller base class, then you will find the following signature of the ViewBag property.
So the ViewBag is a dynamic property of the Controller base class. The dynamic type is introduced in C# 4.0. It is very much similar to the var keyword that means we can store any type of value in it but the type will be decided at run time rather than compile-time.
The ViewBag transfers the data from the controller action method to a view only, the reverse is not possible.
How to Pass and Retrieve data From ViewBag in ASP.NET Core MVC?
The point that you need to keep in mind is, ViewBag is operating on the dynamic data type. So we don’t require typecasting while accessing the data from a ViewBag. It does not matter whether the data that we are accessing is of type string or any complex type.
ViewBag in ASP.NET Core MVC with String Type:
ViewBag in ASP.NET Core MVC with Complex Type:
Example of ViewBag in ASP.NET Core MVC:
Let us see an example to understand how to use ViewBag to pass data from a controller to a view. We are going to work with the same example that we worked in our previous article with ViewData. So, modify the Details action method of HomeController class as shown below.
using FirstCoreMVCApplication.Models; using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { public class HomeController : Controller { public ViewResult Details() { ViewBag.Title = "Student Details Page"; ViewBag.Header = "Student Details"; Student student = new Student() { StudentId = 101, Name = "James", Branch = "CSE", Section = "A", Gender = "Male" }; ViewBag.Student = student; return View(); } } }As you can see in the above example, here we are using the dynamic properties Title, Header, and Student on the ViewBag.
Accessing the ViewBag in a View in ASP.NET Core MVC
Now we will see how to access the ViewBag data within an ASP.NET Core MVC view. So, modify the Details.cshtml view file as shown below.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>@ViewBag.Title</title> </head> <body> <h1>@ViewBag.Header</h1> @{ var student = ViewBag.Student; } <div> StudentId : @student.StudentId </div> <div> Name : @student.Name </div> <div> Branch : @student.Branch </div> <div> Section : @student.Section </div> <div> Gender : @student.Gender </div> </body> </html>As you can see, here we are accessing the data from the ViewBag using the same dynamic properties Title, Header, and Student. Now run the application and navigate to the “/Home/Details” URL and you will see the data as expected on the webpage as shown in the below image.
The ViewBag is a dynamic property that is resolved at runtime; as a result, here also it will not provide compile-time error checking as well as intelligence support. For example, if we miss-spell the property names of the ViewBag, then we wouldn’t get any compile-time error rather we came to know about the error at runtime.
Difference between ViewData and ViewBag in ASP.NET Core MVC
- In ASP.NET Core MVC, we can use both ViewData and ViewBag to pass the data from a Controller action method to a View.
- The ViewData is a weakly typed dictionary object whereas the ViewBag is a dynamic property. Both ViewData and ViewBag are used to create a loosely typed view in MVC.
- In ViewData, we use string keys to store and retrieve the data from the ViewData dictionary whereas in ViewBag we use the dynamic properties to store and retrieve data.
- Both the ViewData keys and ViewBag dynamic properties are resolved only at runtime. As a result, both do not provide compile-time error checking and because of this, we will not get intelligence support.
- So if we misspell the key names or dynamic property names then we will not get any compile-time error rather we came to know about the error only at run time. This is the reason why we rarely used ViewBag and ViewData in our application.
In the next article, I am going to discuss the Strongly Typed Views in ASP.NET Core MVC application with an example. Here, in this article, I try to explain ViewBag in ASP.NET Core MVC application. I hope this article will help you with your needs.
Summary:
I hope this post will be helpful to understand the concept of ViewBag in ASP.NET Core MVC
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.