ASP.NET MVC3中的ViewBag動態性

在MVC 3中的有一個新的ViewBag 動態特性,它主要是為了從Controller到view進行傳值用的,類似有所使用的ViewData[] 字典類。對于ViewBag是如此的強大,意味著你能動態的set/get? 值,增加任何數量的的額外字段而不需要強類型的檢測。

為了看這些的不同(和不用viewbag的時候),我們來看看一下的實例:

Eample:--using ViewBag

Controller

?

view plaincopy to clipboardprint?
  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)

view plaincopy to clipboardprint?
  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

view plaincopy to clipboardprint?
  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)

view plaincopy to clipboardprint?
  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來檢索它們。

轉載于:https://www.cnblogs.com/bobo41/archive/2013/03/22/2975858.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274319.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274319.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274319.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ux設計中的各種地圖_UX設計中的空白

ux設計中的各種地圖UX設計中的空白是什么&#xff1f; (What is white space in UX design?) This article will help you learn about white space and why it so important in UX design.本文將幫助您了解空白以及為什么空白在UX設計中如此重要。 White space is a very us…

花點時間了解消息,句柄和窗口

消息、句柄和窗口 一&#xff0c;為什么我要花時間研究這些東西 對于我個人來說我不想做碼農&#xff0c;不想只知道拖控件&#xff0c;使用控件有的時候要了解一下底層&#xff0c;不知道你們有沒有想過這樣的一個問題&#xff0c;我們在C#中使用的的event事件是怎么觸發這個事…

ux設計中的各種地圖_如何在UX設計中使用顏色

ux設計中的各種地圖Color is the mother tongue of the subconscious.顏色是潛意識的母語。 — Carl Jung—榮格 Color has more to say than meets the eye. As a building block of visual language, color highlights, guides attention, and unifies elements. In UX, co…

《Two Dozen Short Lessons in Haskell》學習(十八) - 交互式鍵盤輸入和屏幕輸出

《Two Dozen Short Lessons in Haskell》&#xff08;Copyright © 1995, 1996, 1997 by Rex Page&#xff0c;有人翻譯為Haskell二十四學時教程&#xff0c;該書如果不用于贏利&#xff0c;可以任意發布&#xff0c;但需要保留他們的copyright&#xff09;這本書是學習 Ha…

figma設計_Figma中簡單,可重復使用的設計系統

figma設計Putting together a design system may seem like an unnecessary hassle. It’s often easier to jump straight into designing things without having to worry about styles, components, or libraries. Some might even argue that when it comes to smaller pro…

WPF 關于鼠標事件和坐標

Mouse.GetPosition(window);可以在任何時間獲得相對任意元素的鼠標位置 Mouse.Capture(el);可以讓某個元素獲得所有的鼠標事件不管他應不應該的到鼠標事件 Mouse.Capture(null);解除轉載于:https://www.cnblogs.com/wangjixianyun/archive/2013/03/25/2980953.html

訪問25%無法訪問的人-如何設計可訪問性

We’re increasingly dependent on the internet and computers for everything we do — this has become starkly more obvious through the COVID19 global pandemic.我們所做的一切都越來越依賴于互聯網和計算機-通過COVID19全球大流行&#xff0c;這一點變得更加明顯。 F…

DDD:實體如何處理外部依賴

場景 修改用戶名時&#xff0c;要驗證用戶名的唯一性。 實現1 1 public class User 2 { 3   public void ChangeUsername(string newUsername) 4   { 5   //使用服務定位器獲取IUsernameUniqueService &#xff0c;執行驗證。 6   } 7 } 實現…

架構師論壇 創業_我在早期創業時作為設計師學到的東西

架構師論壇 創業For over 2 years at a young product company, I collaborated with talented engineering folks for a 0 → 1 suite of products. Here are my learnings and key takeaways. Of course, these are my views and do not represent those of my employers or …

HFileOutputFormat與TotalOrderPartitioner

最近需要為一些數據增加隨機讀的功能&#xff0c;于是采用生成HFile再bulk load進HBase的方式。 運行的時候map很快完成&#xff0c;reduce在sort階段花費時間很長&#xff0c;reducer用的是KeyValueSortReducer而且只有一個&#xff0c;這就形成了單reducer全排序的瓶頸。于是…

qt按鈕禁用和激活禁用_為什么試探法只是經驗法則:禁用按鈕的情況

qt按鈕禁用和激活禁用Most user experience designers will be familiar with Jackob Nielsen’s 10 usability heuristics. They are widely cited and a great set of broad rules of thumb to follow when designing user interfaces.大多數用戶體驗設計師將熟悉Jackob Niel…

Teach Yourself Java 2 in 21 Days 書中樣例代碼實踐

找了好幾書JAVA的書&#xff0c;看了幾章&#xff0c;都看不下去。 我覺得適合《Teach Yourself Java 2 in 21 Days》&#xff08;Rogers Cadenhead Laura Lemay&#xff09;還是適合我的。 孫衛琴那本&#xff0c;我感覺就羅嗦多了沒到我點子上。 接口&#xff0c;抽象類這些內…

好奇心機制_好奇心問題

好奇心機制For my past two jobs I’ve posted a question every week in my team chat and learned so much about my co-workers. Give it a try! :D對于過去的兩個工作&#xff0c;我每周都會在團隊聊天中發布一個問題&#xff0c;并且對我的同事了解很多。 試試看&#xff…

20130328java基礎學習筆記-循環結構for以及for,while循環區別

1.循環結構:for講解class ForDemo{ public static void main(String[] args) { /* for(初始化表達式;循環條件表達式;循環后的操作表達式) { 執行語句;(循環體) } */ for(int x 1; x<3; x) { …

小程序設計避免犯什么錯_新設計師犯下的5種印刷錯誤以及如何避免

小程序設計避免犯什么錯Over the last year and a half, I’ve had the opportunity to teach the basics of typography to undergraduate graphic design students. During this time, I’ve noticed some common mistakes that my students make when first learning how to…

移動設備web文字單位_移動設備如何塑造現代Web設計

移動設備web文字單位I was working with a nonprofit earlier this month on redesigning their website and during the first meeting, I proposed a very standard idea: the home page needed to tell a story and guide the intended user through the intended process (…

hp-ux修改時區方法_UX研究人員可以倡導人類的6種方法

hp-ux修改時區方法In the UX world, we often hear terms like “user-centered,” “human-centered,” and “customer-centered.” We believe that in order to be innovative, we need to center experiences that are authentic, intuitive, and practical.在UX世界中&am…

2013年3月百度之星A題

偽隨機數生成器 題目描述 baidu熊最近在學習隨機算法&#xff0c;于是他決定自己做一個隨機數生成器。 這個隨機數生成器通過三個參數c, q, n作為種子, 然后它就可以通過以下方式生成偽隨機數序列&#xff1a; m0 c, mi1 (q2mi 1) mod 2n, for all i > 0. 因為一些奇怪的…

為什么張揚的人別人很討厭_為什么每個人總是討厭重新設計,即使他們很好

為什么張揚的人別人很討厭重點 (Top highlight)微處理 (Microprocessing) In Microprocessing, columnist Angela Lashbrook aims to improve your relationship with technology every week. Microprocessing goes deep on the little things that define your online life to…

轉載--C語言:浮點數在內存中的表示

單精度浮點數&#xff1a; 1位符號位 8位階碼位 23位尾數 雙精度浮點數&#xff1a; 1位符號位 8位階碼位 52位尾數 實數在內存中以規范化的浮點數存放&#xff0c;包括數符、階碼、尾數。數的精度取決于尾數的位數。比如32位機上float型為23位 double型為52位。…