Wednesday, 20 May 2015

ViewData and ViewBag in Asp.Net mvc

In MVC  there are 3 way to pass the data from controller to view
1.   ViewData,
2.   ViewBag,
3.   TempData ,
We can also use session for passing the data from controller to view , every property has its own importance.

ViewData:-  
1.   Basically ViewData is a directory which is derived from  the ViewDataDictonary class.
public ViewDataDictionary ViewData { get; set; }

2.    it  is a property of  “ControllerBase” class.
3.   It is used to passing the data from Controller to respected view.
4.   Its life lies for the current request.
5.   Its need typecasting for getting the data because it can’t handle null so for checking the null value for avoiding error.
6.   It is similar to Application state or Session state for passing the data.
//Controller code

public ActionResult Index()
{
      List<string> Courses = new List<string>();
     Courses.Add("COMPUTER SCIENCE");
     Courses.Add("DOT NET");
     Courses.Add("C++");
     Courses.Add("INFORMATION TECHNOLOGY");

      ViewData[“Courses"] = Courses;
      return View();
}
//View code
<ul>
    <% foreach (var Courses in ViewData["Courses"as List<string>)
        { %>
    <li><%: Courses %></li>
    <% } %>
</ul>


Or we can write it with different way also
//Controller code
public ActionResult Index()
        {
            ViewData["Courses"] = new List<string>()
    {
        "COMPUTER SCIENCE",
        "DOT NET",
        "C++",
        "INFORMATION TECHNOLOGY"
    };
            return View();
        }


//View Code

@{
    ViewBag.Title = "Courses Name";
}

<h2>Courses Name</h2>
<ul>

@foreach (string Courses in (List<string>)ViewData["Courses"])
{
    <li>@Courses</li>
}

</ul>


This view needs a type casting.

View Bag :-
1.   viewBag is a dynamic property which is introduced in C# 4.0.
2.   It  is also used to pass the data from the controller to view.
public Object ViewBag { get; }

3.   It   is the property of controller base class.
4.   Its also life lies for only for current request.
5.   There is no need for typecasting.

public ActionResult Index()
{
           List<string> Courses = new List<string>();
     Courses.Add("COMPUTER SCIENCE");
     Courses.Add("DOT NET");
     Courses.Add("C++");
     Courses.Add("INFORMATION TECHNOLOGY");

      ViewBag.Courses = Courses;
      return View();
//page code
<ul>
    <% foreach (var Courses in ViewBag.Courses)
        { %>
    <li><%: Courses%></li>
    <% } %>
</ul>

   //Controller code

    public ActionResult Index()
        {
            ViewBag.Courses = new List<string>()
    {
        "COMPUTER SCIENCE",
        "DOT NET",
        "C++",
        "INFORMATION TECHNOLOGY"
    };
            return View();
        }

//View Code
@{
    ViewBag.Title = "Courses Name";
}

<h2>Courses Name</h2>
<ul>

@foreach (string Courses in ViewBag.Courses)
{
    <li>@Courses</li>
}

</ul>

TempData : -

1.   TempData  is a Directory which is derived from the TempDataDictionary.
public TempDataDictionary TempData { get; set; }

2.   It   is the property of controller base class.
3.   It is used to pass the data from controller to view.
4.   Its life is very short means its lies till the respected view fully loaded.
5.   There is need for typecasting.
6.   It is used to store one time message ex. Validation message or error message.
//Controller Code
public ActionResult Index()
{


           List<string> Courses = new List<string>();
     Courses.Add("COMPUTER SCIENCE");
     Courses.Add("DOT NET");
     Courses.Add("C++");
     Courses.Add("INFORMATION TECHNOLOGY");

    TempData["Courses"] = Courses;
    return View();
}
//page code
<ul>
    <% foreach (var Courses in TempData["Courses"as List<string>)
        { %>
    <li><%: Courses %></li>
    <% } %>
</ul>


Session
1.    Session is a property of Controller base class which type is HttpSessionStateBase.
public HttpSessionStateBase Session { get; }


2.   it is also used to pass data Unlike TempData, it persists for its expiration time (by default session expiration time is 20 minutes but it can be increased).
3.   it is valid for all requests, not for a single redirect.
4.   It’s also need typecasting for getting data and for  checking null values to avoid error.


No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...