ViewBag in ASP.NET Core
ViewBag: ViewBag is a dynamic property of the Controller class, used to send a small amount of temporary data from a controller to a view. A controller may have many action methods, but ViewBag only sends data to the view of the action method where it is set.
ViewBag has no compile-time error checking, because it uses the dynamic type, which is resolved at run time based on the data. ViewBag does not need typecasting.
Note: ViewBag can only send data from a controller to a view; it cannot send data from a view to a controller. After a redirect, the value of ViewBag is always null.
How to Pass and Retrieve Data with ViewBag in ASP.NET Core
public class TestController : Controller
{ private readonly AppDbContext db;
public TestController(AppDbContext _db)
{
db = _db;
}
public IActionResult Index()
{
var dropdownItems = db.tblCountries.ToList();
ViewBag.Topictitle = "ViewBag Demo";
ViewBag.TotalRecordsCount = dropdownItems.Count;
ViewBag.Country = new SelectList
(dropdownItems, "CountryId", "CountryName");
return View();
}
}
In the screenshot above, I created a TestController with an Index action method. In the Index method, I fetch the country records from the tblCountries table in the SQL database and assign them to a variable. Then I store this data in a dynamic ViewBag property, Country. I also created two more ViewBag properties: Topictitle, which stores the topic title, and TotalRecordsCount, which stores the number of records in the drop-down list. Now create a view for the Index method, as shown in the image below.
In the screenshot above, I read the ViewBag data and used it in the heading tag. To read the values, I used ViewBag.Topictitle, ViewBag.TotalRecordsCount and ViewBag.Country without any typecasting. With ViewBag there is no need to typecast, whether it holds a string or a complex type such as a list or an object. Run the application, and you can see the final output in the image below.
No typecasting is needed to read data from ViewBag, as shown in the image below.
When to use ViewBag and when not to use it
- Use it when you need to send a small amount of data from a controller to a view.
- Use it when you need to display error messages or notifications in a view.
- Use it to send dynamically created data that is not part of the model class.
- Use it when you need to set layout properties dynamically.
- Avoid ViewBag when you need to send a large amount of data or complex data sets, because it is very difficult to debug; use strongly typed view models instead.
- Don't use it when you need compile-time error checking.
- Don't use it when the same data needs to be reused in many places.
Advantages of ViewBag in ASP.NET Core
- Because ViewBag is dynamic, you don't need to define a model for a small amount of data, which reduces coding effort.
- It is easy to implement and to read data from, with no typecasting.
- We can store any kind of data in it, so it is flexible in many cases.
- It is good for applications that don't need many models.
Disadvantages of ViewBag in ASP.NET Core
- There is no compile-time error checking, so it leads to run-time errors that are difficult to debug.
- There is no IntelliSense support, so development is slower and errors are more likely.
- Run-time binding of the dynamic property adds some performance overhead and slows the application down.
- It is not suitable for complex data structures that need more manipulation and operations.