• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Sunday, August 30, 2020

ViewData in ASP.NET Core MVC

 Admin     August 30, 2020     .Net, .Net Core, .Net Core MVC, Asp.Net, C#     No comments   

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.
ViewData in ASP.NET Core MVC

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 String Data from ViewData in ASP.NET Core MVC

Accessing Student Data:
Accessing Student Data from ViewData in ASP.NET Core MVC

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.
ViewData in ASP.NET Core MVC

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 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Implementation of Dependency Injection Pattern using Methods in C#
    Hi friends! Today we will try to understand another pattern for implementing Dependency Injection called Function Injection . In our last se...
  • ASP.NET State Management
    State management is a technique or way to maintain / store the state of an Asp.Net controls, web page information, object/data, and user in ...
  • Navigation Menus in ASP.NET Core
    In this article, I am going to discuss how to create Responsive Navigation Menus in ASP.NET Core Application using bootstrap and JQuery. P...
  • Introduction to ASP.NET Core Framework
    In this article, I am going to give you a brief introduction to ASP.NET Core Framework . Nowadays, when it comes to software development, e...
  • Swapping Program in C# with Examples
    In this article, I am going to discuss the Swapping Program in C# with and without using third variables. In many C#.NET interviews, the i...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • Implementing basic authentication in ASP NET Web API
    Overview: In this post I am going to explain to how to implement basic authentication using custom authentication filter. Here am not using...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape