Tuesday, June 19, 2012

MVC- ViewData and ViewBag


Difference between ViewData and ViewBag

Both objects ViewData and ViewBag used to pass data from Controller to View and From View to Controller.
ViewData is a dictionary object where you can put data, which then becomes available to the view.
ViewData is a derivative of the ViewDataDictionary class, so you can access by the familiar “key/value” syntax.

ViewBag uses the dynamic feature and it  allows an object to dynamically have properties added to it.
As ViewBag is a dynamic property, instead of writing ViewData[“Value”]=”someValue”, we can write ViewBag.Value=”SomeValue”.
 We can just get or set properties and it will resolve them dynamically at run time.
ViewBag internally uses ViewData to store the values,
 ViewBag properties are stored as name/value pairs in the ViewData dictionary.
Note :  Values stored in ViewData can be accessed by ViewBag and  vice versa.

Example below

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcTextApplication.Controllers
{
    public class ViewBagController : Controller
    {
       
        public ActionResult VDataSample1()
        {
            List<string> Prices = new List<string>();
            Prices.Add("109.09");
            Prices.Add("110.98");
          
            ViewData["Prices"] = Prices;
            ViewData["FundName"] = "US Microcap";
            ViewData["FundClass"] = "Class A";
         
            List<string> Currency = new List<string>();
            Currency.Add("USD");
            Currency.Add("GBP");
            Currency.Add("EURO");
            ViewBag.CurrenyList = Currency;

            ViewBag.FundDate = DateTime.Now.ToString(); 
        

            return View();
        }

    }
}


View

<h2>Simple View data Sample</h2>
<p>
    <b style="color:Red"><u> Fund Details using View Data</u></b><br />
    <b>Fund Name  :  <%: ViewData["Fund Name"]%></b><br />
    <b>Fund Class :  <%: ViewData["FundClass"]%></b><br />
    <b>FundDate(storen in ViewBag) :  <%: ViewData["FundDate"]%></b>     
</p>

<b style="color:Red"><u> Fund Prices</u></b>
 
<ul id="Funds">
<% foreach (var price in ViewData["Prices"] as List<string>)
   { %>
    <li>
       <%: price %>
    </li>
<% } %>
</ul>

<b style="color:Red"><u> View Bag Currency LIST</u></b>
<ul id="Ul2">
<% foreach (var cur in ViewData["CurrenyList"] as List<string>)
   { %>
    <li>
       <%: cur%>
    </li>
<% } %>
</ul>

<hr />

<p>
    <b style="color:Red"><u> Fund Details using ViewBag</u></b><br />
    <b>Fund Name  :  <%: ViewBag.FundName %></b><br />
    <b>Fund Class : <%: ViewBag.FundClass%></b><br /> 
    <b style="color:Red"><u> Fund Prices</u></b>
</p>

<ul id="Ul1">
<% foreach (var price in ViewBag.Prices)
   { %>
    <li>
       <%: price %>
    </li>
<% } %>
</ul>
<b style="color:Red"><u>Currency List</u></b>
<ul id="Ul3">
<% foreach (var cur in ViewBag.CurrenyList)
   { %>
    <li>
       <%: cur %>
    </li>
<% } %>
</ul>



OutPut :


1 comment :

  1. great work..Lavi..daily reader of

    http://paradise-coding.blogspot.in

    ReplyDelete