Asp.NetMVCのViewDataとViewBag


Asp.NetMVC 3 webアプリケーションでは、ViewDataとViewBagを使用します.比較してみましょう.
ViewData
ViewBag
Key/Value辞書の集合です
dynamicタイプの対像です
AspからNetMVC 1はあります
ASP.NET MVC 3ならでは
Aspに基づく.net 3.5 framework
Aspに基づく.Net 4.0と.net framework
ViewDataはViewBagより速い
ViewはViewより遅い
ViewPageでデータを問い合わせるときに適切なタイプを変換する必要があります
ViewPageでのデータの問合せにはタイプ変換は必要ありません
いくつかのタイプの変換コードがあります
読みやすさの向上
ControllerでViewDataを使用するには:
public ActionResult UsingViewData()
{
    
    ViewData["Title"] = " Using ViewData";
    ViewData["ProjectName"] = "My Test Project";
    ViewData["ProjectDescription"] = "This is Test Project to demo Viewdata and viewbag details";
    ViewData["StartDate"] = new DateTime(2011, 1, 1);
    ViewData["TotalPrice"] = 1000;
    ViewData["TotalDays"] = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client","Mr.  Client");
    stackholder.Add("Manager","Mr. Joy");
    stackholder.Add("Team Leader","Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewData["stackholder"] = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewData["modules"] = modules;
    return View();
}

UsingViewData Viewに対応するcshtml:
<h1>@ViewData["Title"]</h1>
 <div>
   <div>
    <h2>Project Name : @ViewData["ProjectName"]</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewData["ProjectDescription"]".</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewData["stackholder"] as Dictionary<string, string> )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewData["modules"] as List<string> )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewData["StartDate"]  <br />
     Project TotalPrice: @ViewData["TotalPrice"] <br />
     Project TotaDays  : @ViewData["TotalDays"] 
   </div>
 </div>

そしてViewBag:
public ActionResult UsingViewBag()
{

    ViewBag.Title = " Using ViewBag";
    ViewBag.ProjectName = "My Test Project";
    ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";
    ViewBag.StartDate = new DateTime(2011, 1, 1);
    ViewBag.TotalPrice = 1000;
    ViewBag.TotalDays = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewBag.stackholder = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewBag.modules = modules;
    return View();
}

ViewUsingViewBagのcshtmlに対応するViewBag:
<h1>@ViewBag.Title</h1>
 <div>
   <div>
    <h2>Project Name : @ViewBag.ProjectName</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewBag.ProjectDescription.</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewBag.stackholder )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewBag.modules )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") <br />
     Project TotalPrice: @ViewBag.TotalPrice  ₹  <br />
     Project TotaDays  : @ViewBag.TotalDays 
   </div>
 </div>

次はControllerでViewBagを使用します.
public ActionResult UsingViewBagInControlAndViewDataInView()
{

    ViewBag.Title = " Using ViewBag In Control And ViewData In View";
    ViewBag.ProjectName = "My Test Project";
    ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";
    ViewBag.StartDate = new DateTime(2011, 1, 1);
    ViewBag.TotalPrice = 1000;
    ViewBag.TotalDays = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewBag.stackholder = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewBag.modules = modules;
    return View();
}

U s i n g w ewBagInControlAndViewDataInView.cshtmlではViewDataを使用して値を取ります.
<h1>@ViewData["Title"]</h1>
 <div>
   <div>
    <h2>Project Name : @ViewData["ProjectName"]</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewData["ProjectDescription"]".</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewData["stackholder"] as Dictionary<string, string> )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewData["modules"] as List<string> )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewData["StartDate"]  <br />
     Project TotalPrice: @ViewData["TotalPrice"] <br />
     Project TotaDays  : @ViewData["TotalDays"] 
   </div>
 </div>

逆に言えば、
public ActionResult UsingViewDataInControlAndViewBagInView()
{
    ViewData["Title"] = " Using ViewData In Control And ViewBag In View";
    ViewData["ProjectName"] = "My Test Project";
    ViewData["ProjectDescription"] = "This is Test Project to demo Viewdata and viewbag details";
    ViewData["StartDate"] = new DateTime(2011, 1, 1);
    ViewData["TotalPrice"] = 1000;
    ViewData["TotalDays"] = 100;
    Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewData["stackholder"] = stackholder;

    List<string> modules = new List<string>();
    modules.Add("Admin module");
    modules.Add("ShoppingCart module");
    modules.Add("CMS module");
    ViewData["modules"] = modules;
    return View();
}

U s i n g W ewDataInControlAndViewBagInView.cshtmlのViewでは、ViewBagを使用して値を取ります.
<h1>@ViewBag.Title</h1>
 <div>
   <div>
    <h2>Project Name : @ViewBag.ProjectName</h2>
   </div>
   <div>
     ProjectDescription :   
     <p>"@ViewBag.ProjectDescription.</p>
   </div>
   <div>
      Stack Holder :
      <br />

      <ul id="stakholder">
      @foreach ( var stakerholder in ViewBag.stackholder )
      {          
    <li>
        @stakerholder.Key &nbsp; : @stakerholder.Value
    </li>
      }
     </ul>
   </div>
   <div>
     Project Details:<br />
     <div>
       module List  :
       <ul id="modulelist">
      @foreach ( var module in ViewBag.modules )
      {          
    <li>
        @module
    </li>
      }
     </ul>
        
     </div>
     Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") <br />
     Project TotalPrice: @ViewBag.TotalPrice  ₹  <br />
     Project TotaDays  : @ViewBag.TotalDays 
   </div>
 </div>

このように比較するとはっきり見えます.ソースコードのViewBagは、次のようなプロパティです.
public dynamic ViewBag {
    get {
        if (_dynamicViewData == null) {
            _dynamicViewData = new DynamicViewDataDictionary(() => ViewData);
        }
        return _dynamicViewData;
    }
}

ViewData Yes:
public ViewDataDictionary ViewData {
    get {
        if (_viewData == null) {
            SetViewData(new ViewDataDictionary());
        }
        return _viewData;
    }
    set {
        SetViewData(value);
    }
}

具体的な細部はここでは深くないが,ここはただレンガを投げて玉を引くだけだ.あなたにNetMVCの開発に役立ちます.