What is ViewBag?
ViewBag is used to pass data from a Controller to a View in an ASP.NET MVC application.
It works like a dynamic object.
No key string required.
You can access it using dot (.) notation.
Why Do We Use ViewBag?
Real-Life Example:
Imagine a teacher directly tells the monitor the student’s name instead of writing it on paper.
It is temporary and works only for that request.
Example:
Controller Code:
public ActionResult Index() { ViewBag.StudentName = "Ali"; ViewBag.Marks = 85; return View(); }
View Code:
<h2> Student Name: @ViewBag.StudentName </h2> <h3> Marks: @ViewBag.Marks </h3>
ViewBag vs ViewData Difference:
| ViewData | ViewBag |
|---|---|
| Dictionary (key–value) | Dynamic object |
| Needs type casting | No type casting required |
| Uses string key | Uses dot notation |
| Older way | Cleaner & modern way |
Inside the Models folder, right-click and select Add → Class. Create a new class file named Student.cs .
This class defines the student properties (such as Name, Address, etc.) and represents the structure of student data used in the application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Your_Web_Application_Name.Models
{
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Course { get; set; }
public int Fees { get; set; }
}
}Inside the Models folder, right-click and select Add → Class. Create a new class file named StudentBusinessLayer.cs .
The StudentBusinessLayer class is responsible for handling the business logic related to students. It retrieves student details and prepares the data to be used by the Controller.
In this class, the GetEmployeeDetails() method accepts a Student ID as a parameter, creates a Student object, assigns values (like Name, Address, Course, and Fees), and returns the student data.
Currently, the data is hardcoded for learning purposes. Later, this can be connected to a database to fetch real student records.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Your_Web_Application__Name.Models
{
public class StudentBusinessLayer
{
public Student GetEmployeeDetails(int StudentId)
{
Student stu = new Student()
{
StudentId = StudentId,
Name = "AIT",
Address = "Aurangabad",
Course = "DFD",
Fees = 45000
};
return stu;
}
}
}First, go to the Controllers folder, right-click on it, and select Add → Controller. Create a controller named HomeController .
Now open HomeController.cs.
Inside the Index method, we create an object of StudentBusinessLayer and get student details.
The student data is stored in ViewBag (for example: ViewBag.Students and ViewBag.Header) and then sent to the View using return View();
Simple: HomeController gets student data and sends it to the View using ViewBag.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Your_Web_Application_Name.Models; //
namespace Your_Web_Application_Name.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
StudentBusinessLayer studentBL = new StudentBusinessLayer();
Student stu = studentBL.GetEmployeeDetails(102);
ViewBag.Studentdata = stu;
ViewBag.Header = "Student Details";
return View();
}
}
}Go to the HomeController, right-click on the Index action method, and select Add View.
This will create the Index.cshtml file inside the Views → Home folder.
This View receives the student data from ViewBag sent by the HomeController .
We access the student object using ViewBag.Students and display the heading using ViewBag.Header.
Then, we show the student details in a table format.
Simple: Right-click on HomeController → Add View → It shows the student data sent from the Controller using ViewBag.
@{
var student1 = ViewBag.Studentdata;
}
<h2>@ViewBag.Header</h2>
<table style="font-family:Arial">
<tr>
<td>Student ID:</td>
<td>@student1.StudentId</td>
</tr>
<tr>
<td>Name:</td>
<td>@student1.Name</td>
</tr>
<tr>
<td>Address:</td>
<td>@student1.Address</td>
</tr>
<tr>
<td>Course:</td>
<td>@student1.Course</td>
</tr>
<tr>
<td>Fees:</td>
<td>@student1.Fees</td>
</tr>
</table>
https://localhost:44300/Home/Index