ASP.NET MVC3中的ViewBag动态性

在MVC 3中的有一个新的ViewBag 动态特性,它主要是为了从Controller到view进行传值用的,类似有所使用的ViewData[] 字典类。对于ViewBag是如此的强大,意味着你能动态的set/get  值,增加任何数量的的额外字段而不需要强类型的检测。

为了看这些的不同(和不用viewbag的时候),我们来看看一下的实例:

Eample:--using ViewBag

Controller

 

  1. public ActionResult Index()    
  2.   
  3.  {    
  4.   
  5.      List<string> colors = new List<string>();    
  6.   
  7.      colors.Add("red");    
  8.   
  9.      colors.Add("green");    
  10.   
  11.      colors.Add("blue");                
  12.   
  13.       
  14.   
  15.     ViewData["listColors"] = colors;    
  16.   
  17.      ViewData["dateNow"] = DateTime.Now;    
  18.   
  19.      ViewData["name"] = "Hajan";    
  20.   
  21.      ViewData["age"] = 25;    
  22.   
  23.       
  24.   
  25.     return View();    
  26.   
  27.  }   

View (ASPX View Engine)

  1. p>    
  2.   
  3. 02      My name is    
  4.   
  5. 03     <b><%: ViewData["name"] %></b>,     
  6.   
  7. 04     <b><%: ViewData["age"] %></b> years old.    
  8.   
  9. 05      <br />        
  10.   
  11. 06      I like the following colors:    
  12.   
  13. 07  </p>    
  14.   
  15. 08  <ul id="colors">    
  16.   
  17. 09  <% foreach (var color in ViewData["listColors"] as List<string>){ %>    
  18.   
  19. 10      <li>    
  20.   
  21. 11         <font color="<%: color %>"><%: color %></font>    
  22.   
  23. 12     </li>    
  24.   
  25. 13  <% } %>    
  26.   
  27. 14  </ul>    
  28.   
  29. 15  <p>    
  30.   
  31. 16      <%: ViewData["dateNow"] %>    
  32.   
  33. 17  </p>   


Eample :-Using ViewBag

Controller

  1. public ActionResult Index()    
  2.   
  3. 02  {    
  4.   
  5. 03      List<string> colors = new List<string>();    
  6.   
  7. 04      colors.Add("red");    
  8.   
  9. 05      colors.Add("green");    
  10.   
  11. 06      colors.Add("blue");    
  12.   
  13. 07       
  14.   
  15. 08     ViewBag.ListColors = colors; //colors is List     
  16.   
  17. 09      ViewBag.DateNow = DateTime.Now;    
  18.   
  19. 10      ViewBag.Name = "Hajan";    
  20.   
  21. 11      ViewBag.Age = 25;    
  22.   
  23. 12      return View();     
  24.   
  25. 13 }   


你和上面的对比 你看见了不同吗?

View (ASPX View Engine)

  1. <p>    
  2.   
  3.      My name is    
  4.   
  5.     <b><%: ViewBag.Name %></b>,     
  6.   
  7.     <b><%: ViewBag.Age %></b> years old.    
  8.   
  9.      <br />        
  10.   
  11.      I like the following colors:    
  12.   
  13.  </p>    
  14.   
  15.  <ul id="colors">    
  16.   
  17.       
  18.   
  19. <% foreach (var color in ViewBag.ListColors) { %>    
  20.   
  21.      <li>    
  22.   
  23.          <font color="<%: color %>"><%: color %></font>    
  24.   
  25.      </li>    
  26.   
  27.  <% } %>    
  28.   
  29.       
  30.   
  31. </ul>    
  32.   
  33.  <p>    
  34.   
  35.      <%: ViewBag.DateNow %>    
  36.   
  37.  </p>   


 

在上面的例子以内ViewBag是动态类型的,所以我没有必要把 ViewBag.ListColors 转换成如 List<string>这样子的形式。另一方面,这个ViewData["key"]是一个对方。

如果你在你的Controller中使用ViewData["ListColors"]=Colors,那么你可以在View试图中通过ViewBag.ListColors来检索它们。

posted @ 2013-03-22 16:36  风雪七月花溅墨  阅读(134)  评论(0编辑  收藏  举报