Passing Data To View In ASP.Net Core MVC

Introduction

In this article, we’ll discuss passing data to a view from a controller in asp.net core MVC. Please, read our previous article before proceeding to this article where we learn Views in ASP.NET Core MVC. So, there are three ways to do this we can either use

1. ViewData
2. ViewBag
3. Strongly Typed View

So, we’ll use the ViewData or ViewBag to pass data from a controller to a view. we create something called a loosely typed view, well this is what loosely typed view in asp.net core.

So, far are index action has just called the view without passing any data.

public IActionResult Index()
{
    User model = _UserRepository.GetUser(1);

    // Pass PageTitle and User model to the View using ViewData
    ViewData["PageTitle"] = "User Details";
    
	//Pass Users list to the View using ViewBag 
	ViewData.Users = model;

    return View();
}

It means when an action calling you it usually passes some data to the view and when you receive the data and generate some HTML based on it. the ASP.Net MVC provides multiple ways to pass data from controller to view in asp.net core.

here, we create user class and also creates a list of users inside our action.

ViewBag in ASP.NET Core MVC

Now, we have three options to pass this needs to be the first option is ViewBag. the ViewBag is a dynamic object. So on the fly, you can give it a property and assign anything to it.

Now that it is assigned to ViewBag in the controller.

Let’s go to View and use it and we can read it like this.

viewbag model in asp.net mvc

But, ViewBag is a dynamic object. So the type we use there is not defined. So, we simply cast to solve this problem something like this.

viewbag in asp.net mvc

Now, we know users variable is there a list of users. And to show our list we added a table.

table list viewdata in asp.net mvc

In this way, we have defined each new row inside arrays that you in this way for view in asp.net core. There we have one row inside the table which shows the ID and the name of that user.

Let’s see the output in the browser. we can see the list of users here.

asp.net .net core mvc output

ViewData in ASP.NET Core MVC

Now, we are going back to our TestController as the second option besides ViewBag we have something similar to it which you already know it. it is viewdata and this is the same code using your data.

viewdata in asp.net core

The difference is when data is a dictionary of why your bag is a dynamic object and the performance of your data is better. But feel free to use either of them based on your programming style.

So if we go back to the razor view and declare viewdata into it.

Now if we save them and see the output in the browser.

razor index in asp.net mvc

Passing object to view

Let’s, back to the controller and see the third option. Then here we can pass users as a parameter to the method.

razor view in asp.net core mvc

the model followed by the type of what we passed to the view method as a parameter.

So, in fact, Model is what is passed to the method as the parameter and the type of model is defined with a model of the first line.

Model Object in asp.net mvc core

Let’s go and check in the browser.

razor index in asp.net mvc

The result is the same. usually, ASP.NET developer’s favorite model.

Thank you for reading this article, I hope you will understand the Passing Data To View In ASP.Net Core MVC. We’ll get into more details about this when we’ll learn Strongly Typed View in ASP.NET Core MVC in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *