Suggested Videos
Part 20 - Controller in ASP.NET Core MVC | Text | Slides
Part 21 - Views in ASP.NET Core MVC | Text | Slides
Part 22 - Customize view discovery in asp.net core mvc | Text | Slides
In this video we will discuss different ways of passing data to a View from a Controller in ASP.NET Core MVC.
Three ways to pass data from a controller to a view
In ASP.NET Core MVC, there are 3 ways to pass data from a controller to a view
By using ViewData or ViewBag to pass data, we are creating a loosely typed view. We will discuss what a loosely typed view means in just a bit.
Using ViewData to pass Data from a Controller to a View
We want to pass Employee model data and a Title for the view page from the Details() action method of the HomeController to the Details.cshtml view. So modify the Details() action method in the HomeController as shown below.
Accessing ViewData in a View
To access the ViewData passed by the Details() action method of the HomeController, modify the code in Details.cshtml view file as shown below.
ViewData
Next Video : Using ViewBag to pass data from the Controller to the View
Part 20 - Controller in ASP.NET Core MVC | Text | Slides
Part 21 - Views in ASP.NET Core MVC | Text | Slides
Part 22 - Customize view discovery in asp.net core mvc | Text | Slides
In this video we will discuss different ways of passing data to a View from a Controller in ASP.NET Core MVC.
Three ways to pass data from a controller to a view
In ASP.NET Core MVC, there are 3 ways to pass data from a controller to a view
- Using ViewData
- Using ViewBag
- Using a strongly typed model object. This is also called Strongly typed view.
By using ViewData or ViewBag to pass data, we are creating a loosely typed view. We will discuss what a loosely typed view means in just a bit.
Using ViewData to pass Data from a Controller to a View
We want to pass Employee model data and a Title for the view page from the Details() action method of the HomeController to the Details.cshtml view. So modify the Details() action method in the HomeController as shown below.
public ViewResult Details()
{
Employee model = _employeeRepository.GetEmployee(1);
// Pass PageTitle and Employee model to the View using ViewData
ViewData["PageTitle"] = "Employee Details";
ViewData["Employee"] = model;
return View();
}
{
Employee model = _employeeRepository.GetEmployee(1);
// Pass PageTitle and Employee model to the View using ViewData
ViewData["PageTitle"] = "Employee Details";
ViewData["Employee"] = model;
return View();
}
Accessing ViewData in a View
To access the ViewData passed by the Details() action method of the HomeController, modify the code in Details.cshtml view file as shown below.
<html>
<head>
<title></title>
</head>
<body>
<h3>@ViewData["PageTitle"]</h3>
@{
var employee = ViewData["Employee"] as EmployeeManagement.Models.Employee;
}
<div>
Name : @employee.Name
</div>
<div>
Email : @employee.Email
</div>
<div>
Department : @employee.Department
</div>
</body>
</html>
<head>
<title></title>
</head>
<body>
<h3>@ViewData["PageTitle"]</h3>
@{
var employee = ViewData["Employee"] as EmployeeManagement.Models.Employee;
}
<div>
Name : @employee.Name
</div>
<div>
Email : @employee.Email
</div>
<div>
Department : @employee.Department
</div>
</body>
</html>
ViewData
- ViewData is a dictionary of weakly typed objects.
- To store and retrieve data from the ViewData dictionary we use string keys.
- String data can be accessed from ViewData dictionary without the need to cast the data to string type.
- If we are accessing any other type of data, we need to explicitly cast it to the type we are expecting.
- In our example, we are casting the Employee object to Employee Type before accessing Name, Email and Department properties of the Employee object.
- ViewData is dynamically resolved at runtime, so it does not provide compile-time type checking and as a result we do not get intellisense.
- Since we do not have intellisense, the speed at which we write code is reduced and the chances of mis-spelling and making typographical errors are also high.
- We will only come to know about these errors at run time.
- For this reason we usually do not use ViewData.
- When we use ViewData, we end up creating a loosely typed view.
Next Video : Using ViewBag to pass data from the Controller to the View
I am getting following exception
ReplyDeletethe name viewdata does not exist in current context
viewdata is case sensitive ..check if its that
Deleteinherit Controller class to homeController
ReplyDelete"Public HomeController:Controller"
it works for me