0% found this document useful (0 votes)
5 views

Passing Data To View in ASP NET Core MVC

Uploaded by

Venu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Passing Data To View in ASP NET Core MVC

Uploaded by

Venu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

How do you Pass Data to Views in ASP.NET Core MVC?

• In ASP.NET Core MVC Applications, we can pass the


data from a controller action method to a view in many
different ways, such as by using ViewBag, ViewData,
TempData, and a Strongly Typed Model.

How to use ViewData to pass the data from the controller


action method to a view.
What is ViewData in ASP.NET Core MVC Application?
• ViewData is a dictionary object in ASP.NET Core
MVC that allows us to pass data from a controller action
method to a view.
• It provides a flexible way to pass data, allowing us to
use key-value pairs.
• ViewData is useful when we need to pass dynamic data
or data that doesn’t fit well into a strongly typed model.
• If you go to the definition of ViewData, you will see that
It is a property in the Controller abstract class, and its
type is ViewDataDictionary.
• This property is also decorated with the
ViewDataDictionary attribute, as shown in the image
below.
As you can see in the above image, the data type of
ViewData Property is ViewDataDictionary.
• As you can see in the above image, the ViewDataDictionary
class implements the IDictionary.
• So, ViewData in ASP.NET Core MVC is a dictionary object.
As a dictionary object, it stores data in the form of Key-Value
Pairs, where each key must be a string, and the value that we
pass to the dictionary will be stored as an object type.
• Here, the key is of type String, and the value is of type object,
i.e., we can store any data, including null.
How do you use ViewData in the ASP.NET Core MVC
Application?
To use ViewData in the ASP.NET Core MVC Application,
we need to set the data in a controller by adding entries to
the ViewData dictionary with a string key and then assign
some data. The key should be in string format, and you can
give any name to the key. Then, you can assign any data to
this key, such as the following.
ViewData[“KeyName”] = “Some Data”;
• Since ViewData is a server-side code, hence to
use it on a view, we need to use the razor syntax,
i.e., @ something as follows.
@ViewData[“KeyName”]
You can access the string data from the ViewData
dictionary without casting the data to string type.
But if you are accessing data other than the string
type, you must explicitly cast the data to the type
you expect.
• How to Access String Data using ViewData in
ASP.NET Core MVC
• How to Access Student Data using ViewData in
ASP.NET Core MVC
Example to Understand ViewData in ASP.NET Core
MVC Application:
• First, create an ASP.NET Core Web Application
(with the name FirstCoreMVCWebApplication)
with MVC (Model-View-Controller) Project
Template, i.e., Creating ASP.NET Core Application
using ASP.NET Core Web APP (Model-View-
Controller) Template.
• Once you create the project, add a class file
named Student.cs in the Models folder.
Modifying HomeController:
• Next, modify the Home Controller class as shown
below.
• Here, we remove the existing code and add one
action method, i.e., Details.
• As part of this method, we create three
ViewData objects to store the Title, Header, and
Student data.
• Here, Title, Header, and Student are the keys of
the ViewData Dictionary Object.
Creating Details.cshtml View:
• Let us add a view named Details.cshtml within the
Home folder, which is inside the Views folder, as shown
below. Once you add the Details view, your Views folder
structure should look as shown below.
• Now open the Details.cshtml view and write the
following code. In the code below, we directly
access the string data from the ViewData
without type casting. However, while accessing
the Student data from the ViewData, we
typecast it to the appropriate Student type.
• To Add View right click on Home/AddView/Razor
View and click on ADD
• Click on Add to create view

You might also like