How to use ViewData and ViewBag in MVC.

Sourav.Kayal
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 13814 red flag

How to use ViewData and ViewBag in MVC application.

How to use ViewData and ViewBag in MVC?

In this article we will learn how to use ViewData and ViewBag in MVC application.We have to choose MVC3 to use ViewBag because MVC and MVC2 does not support it. Both ViewData and ViweBag are used to pass data from Controller to view. Let’s see how to use them with simple example.

How to use ViewData ?

To understand the concept of ViewData, we have to create one sample MVC project. Add below code for Model, Controller and View.

Model class

Here is simple model class named Person with two properties.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace MvcApplication1.Models
{
 
    public class Person
    {
        public String Name { get; set; }
        public String Surname { get; set; }
    }
}
 

Controller Class

This is our controller class. GetPerson() is action within controller class it will invoke view. And before invoking it will assign object of  Person class into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1;
using MvcApplication1.Models;
 
namespace MvcApplication1.Controllers
{
    public class PersonController : Controller
    {
        public ActionResult GetPerson()
        {
            Person p = new Person();
            p.Name = "Sourav";
            p.Surname = "Kayal";
            ViewData["Person"] = p;
            return View();           
        }
    }
}
 

View

Lets create and consume ViewData from view. Sample code is in below.

<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Person>"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>GetPerson</title>
</head>
<body>
    <div>
    <% var Value =(MvcApplication1.Models.Person) ViewData["Person"];%>
    <table>
    <tr>
        <td>Name:- </td>
        <td><%= Value.Name %></td>
    </tr>
        <tr>
        <td>Surname:- </td>
        <td><%= Value.Surname %></td>
    </tr>
    </table>
    </div>
</body>
</html>
 

Here is sample output.

 

How to use ViewBag?

We have to create one MVC3 application to understand the concept of ViewBag. Open one new MVC3 project and all below component.

Model class

Model class is nothing but entity of application. We have created simple Person model in below example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace MVC3.Models
{
    public class Person
    {
        public String Name { get; set; }
        public String Surname { get; set; }
    }
}

Controller class

Here is our controller class. It will handle event. We have defined ShowPerson() action within Person controller class. Here we are assigning value to ViewBag and we will consume this data from View .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3.Models;
 
namespace MVC3.Controllers
{
    public class PersonController : Controller
    {
        public ActionResult ShowPerson()
        {
            Person p = new Person();
            p.Name = "sourav";
            p.Surname = "Kayal";
            ViewBag.Value = p;
            return View();
        }
    }
}
 

View page

At last we have to create view to consume ViweBag data.

<%@ Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage<dynamic>"
%>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>ShowPerson</title>
</head>
<body>
    <div>
        <% var val =(MVC3.Models.Person) ViewBag.Value; %>
        Name is:- <%= val.Name%>  <br />
Surnam is:- <%= val.Surname%>
    </div>
</body>
</html>

Here is sample output           


Conclusion:-

In this article we have discussed on how to use ViewData and ViewBag with simple example. Hope you have understood the concept.

 

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 9/5/2013 | Points: 25
Nice article Sourav....keep going !

Akiii
Posted by: Sourav.Kayal on: 9/5/2013 | Points: 25
Thank you Akiii.
Posted by: Coursedloadr on: 9/7/2013 | Points: 25
Good and informative.

Login to post response

Comment using Facebook(Author doesn't get notification)