Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> MVC >> ViewData in ASP.NET MVC
ViewData in ASP.NET MVC
ViewData in ASP.NET MVC


by Rohit Kesharwani on 10/1/2011 2:48:13 PM

Views: 1826       Comments: 0

ViewData in ASP.NET MVC

Viewdata is the collection of state that is passed to a view. A view should get all the information it needs from the viewdata. It contains key/value pairs as well as some special properties, such as Model. Viewdata is accessible on the controller as well. The controller is responsible for filling the viewdata dictionary with objects. When a view is executing, it will pull various object from viewdata while it is rendering. If an expected object is not present, then you will see the same type of exception present in any code using a dictionary collection in .Net.

How to use View Data?


Here, I am going to explain how to use ViewData in View to get the information dynamically through Controller and Model.

In HomeController.cs, we set the Current Date and Time by using Now property of DateTime class in the field of ViewData property.

using System;

using System.Web.Mvc;

using ViewData.Models;

 

namespace ViewData.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            ViewData["CurrentTime"] = DateTime.Now.ToString(); //set the current date and
                                                                 time in ViewData field

            return View(new ModelClass());

        }

    }

}

 

In ModelClass.cs, we created a property named as FirstName that return a name.

namespace ViewData.Models

{

    public class ModelClass

    {

        public string FirstName

        {

            get

            {

                return "Rohit";

            }

        }

    }

}

 

After setting the data in ViewData, here in View we retrieved the information in View through ViewData.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ViewData.Models.ModelClass>" %>

 

<!DOCTYPE html>

 

<html>

<head runat="server">

    <title>Index</title>

</head>

<body>

    <div>

                <!--Getting data from Model ViewData -->

        <h3>Hello! <%: ViewData.Model.FirstName %> <br /><br />

 

              <!--Getting data from Controller View Data -->

        The current date and time is <%: ViewData["CurrentTime"] %></h3>

    </div>

</body>

</html>

 

The above code display output like below:

ViewData in ASP.NET MVC

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2747
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.