Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Passing data to view in ASP.NET Core MVC

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
  1. Using ViewData
  2. Using ViewBag
  3. 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();
}

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>

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

asp.net core tutorial for beginners

3 comments:

  1. I am getting following exception
    the name viewdata does not exist in current context

    ReplyDelete
    Replies
    1. viewdata is case sensitive ..check if its that

      Delete
  2. inherit Controller class to homeController
    "Public HomeController:Controller"
    it works for me

    ReplyDelete

It would be great if you can help share these free resources