In this article, I am going to discuss the use of ViewData in ASP.NET Core MVC application with examples. Please read our previous article before proceeding to this article where we discussed Views in ASP.NET Core MVC application.
In ASP.NET Core MVC application, 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 using strongly typed model. In this article, I will show you 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?
The ViewData in MVC is a dictionary of weakly typed objects which is derived from the ViewDataDictionary class.
As the ViewData is a dictionary object, so it will store the data in the form of key-value pairs where each key must be a string. 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 then you need to explicitly cast the data to the type you are expecting.
For Example: To access string data
Accessing Student Data:
ViewData Example in ASP.NET Core MVC Application:
Let us see an example to understand how to use view data to pass data from a controller action method to a view.
In our example, we want to pass three pieces of information to the view from the controller. One is the Title of the page, second is the Header of the Page and the third one is the Student data that we want to show on the page.
So, modify the Details action method of the Home Controller as shown below.
In order to access the ViewData, modify the Details.cshtml view file as shown below.
The ViewData is dynamically resolved at runtime, as a result, it does not provide compiles time error checking as well as we do not get any IntelliSense. For example, if we miss-spell the key names then we wouldn’t get any compile-time error. We get to know about the error only at runtime
The ViewData only transfers the data from the controller action method to a view, but not vice-versa. That means it is valid only during the current request.
In the next article, I will discuss ViewBag in ASP.NET Core MVC with an example. In this article, I try to explain ViewData in ASP.NET Core MVC application. I hope this article will help you to understand the ViewData in ASP.NET Core MVC Application.
Summary:
I hope this post will be helpful to understand the concept of ViewData in ASP.NET Core MVC
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
In ASP.NET Core MVC application, 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 using strongly typed model. In this article, I will show you 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?
The ViewData in MVC is a dictionary of weakly typed objects which is derived from the ViewDataDictionary class.
As the ViewData is a dictionary object, so it will store the data in the form of key-value pairs where each key must be a string. 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 then you need to explicitly cast the data to the type you are expecting.
For Example: To access string data
Accessing Student Data:
ViewData Example in ASP.NET Core MVC Application:
Let us see an example to understand how to use view data to pass data from a controller action method to a view.
In our example, we want to pass three pieces of information to the view from the controller. One is the Title of the page, second is the Header of the Page and the third one is the Student data that we want to show on the page.
So, modify the Details action method of the Home Controller as shown below.
using FirstCoreMVCApplication.Models; using Microsoft.AspNetCore.Mvc; namespace FirstCoreMVCApplication.Controllers { public class HomeController : Controller { public ViewResult Details() { //String string Data ViewData["Title"] = "Student Details Page"; ViewData["Header"] = "Student Details"; Student student = new Student() { StudentId = 101, Name = "James", Branch = "CSE", Section = "A", Gender = "Male" }; //storing Student Data ViewData["Student"] = student; return View(); } } }Accessing the ViewData from a View in ASP.NET Core MVC.
In order to access the ViewData, modify the Details.cshtml view file as shown below.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>@ViewData["Title"]</title> </head> <body> <h1>@ViewData["Header"]</h1> @{ var student = ViewData["Student"] as FirstCoreMVCApplication.Models.Student; } <div> StudentId : @student.StudentId </div> <div> Name : @student.Name </div> <div> Branch : @student.Branch </div> <div> Section : @student.Section </div> <div> Gender : @student.Gender </div> </body> </html>Now run the application and navigate to the “/Home/Details” URL and you will see the data as expected as shown below.
The ViewData is dynamically resolved at runtime, as a result, it does not provide compiles time error checking as well as we do not get any IntelliSense. For example, if we miss-spell the key names then we wouldn’t get any compile-time error. We get to know about the error only at runtime
The ViewData only transfers the data from the controller action method to a view, but not vice-versa. That means it is valid only during the current request.
In the next article, I will discuss ViewBag in ASP.NET Core MVC with an example. In this article, I try to explain ViewData in ASP.NET Core MVC application. I hope this article will help you to understand the ViewData in ASP.NET Core MVC Application.
Summary:
I hope this post will be helpful to understand the concept of ViewData in ASP.NET Core MVC
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.