SlideShare a Scribd company logo
1 of 11
MVC-Passing Data
Vic Chih
2016-03-25
大綱
• MVC
– Controller to View
– ViewData
– ViewBag
– TempData
– View Model
– Tuple
Controller to View
• ViewData
– ViewDataDictionary字典型別
– Public class ViewDataDictionary : IDictionary<string,object>{}
– 必須要有string做Key值
– 無法跨action做存取
– ViewData[“text”] = “Hello MVC”;
Controller to View
• ViewBag
– 與ViewData雷同
– dynamic型別
– 物件.屬性
– 在Action呼叫view page時自動傳遞過去
Action
Public ActionResult demo()
{
ViewBag.items = _dc.abc.ToList();
Return View();
}
View
@foreach(var item in ViewData[“items”] )
Controller to View
• TempData
– Action傳Action的時候 可以使用
– 亦是繼承與實作IDictionary
– 使用session做資料傳遞
– 透過RedirectToAction導頁到不同的Action時,則可以取得到上一個Action所存放
在TempData的資料
– 在一個要求流程中被取出就會被刪除
Controller to View
• TempData
他是從ASP.NET執行環境一個名稱為
TempData的暫存資料中取資料而非
TempDataDictionary
因為在Source Code中會先判斷若是
子action的方法會直接從TempData
抓取資料而不會刪除session的資料
如果不希望從TempDataDictionary
取資料會被刪除的話使用keep()
Controller to View
• View Model
– 大型專案往往需要同時投遞多個Model與物件
– 一個View 只能接收一個model陳述式
– ViewModel包含的屬性可以用來呈現(label for、editor for…)
– View Model 可以包含多個不同的model 多個實體或物件
Controller to View
• Tuple
– 省下建置ViewModel的時間
– 在View Page亦是強行別
– 可讀性低
強弱型別的差異
• 弱型別
– 沒有型別檢查
– 沒有IntelliSense
– 編譯時期無法除錯
– Run time 才能確認是否無誤
– 執行實情才能得知型別資訊
• 強行別
– 編譯時期即能發現錯誤
– 變數能夠被IntelliSense支援
– 執行的速度也較快
Reference
• MVC5 網站開發美學
• http://techfunda.com/howto/asp-net-mvc
Thank you for your listening

More Related Content

Viewers also liked

S_Dinakaran_RHCSA
S_Dinakaran_RHCSAS_Dinakaran_RHCSA
S_Dinakaran_RHCSAdinakaran s
 
Sc method1
Sc method1Sc method1
Sc method1roshanrm
 
πληροφορικη Nefeli kathrine
πληροφορικη Nefeli kathrineπληροφορικη Nefeli kathrine
πληροφορικη Nefeli kathrineKNM2000
 
Lapata rewa janpad nrega me hera pheri7
Lapata rewa janpad nrega me hera pheri7Lapata rewa janpad nrega me hera pheri7
Lapata rewa janpad nrega me hera pheri7Yograj Tiwari
 
Learning styles john faught
Learning styles john faughtLearning styles john faught
Learning styles john faughtjefaught
 
Užívateľské testovanie a sledovanie užívateľov
Užívateľské testovanie a sledovanie užívateľovUžívateľské testovanie a sledovanie užívateľov
Užívateľské testovanie a sledovanie užívateľovWebcreator.sk
 
X tensive dpa - мониторинг станков с ЧПУ университетам.
X tensive dpa - мониторинг станков с ЧПУ университетам.X tensive dpa - мониторинг станков с ЧПУ университетам.
X tensive dpa - мониторинг станков с ЧПУ университетам.Igor Tretyakov
 
PetroSync - Pre-Commissioning, Commissioning and Start-Up
PetroSync - Pre-Commissioning, Commissioning and Start-UpPetroSync - Pre-Commissioning, Commissioning and Start-Up
PetroSync - Pre-Commissioning, Commissioning and Start-UpPetroSync
 
Илья Коган, Яндекс.Маркет
Илья Коган, Яндекс.МаркетИлья Коган, Яндекс.Маркет
Илья Коган, Яндекс.МаркетInSales
 
Сергей Малышев Почта России eRetailForum2015
Сергей Малышев Почта России eRetailForum2015Сергей Малышев Почта России eRetailForum2015
Сергей Малышев Почта России eRetailForum2015InSales
 
Back to Basic
Back to BasicBack to Basic
Back to BasicWil Post
 

Viewers also liked (13)

mercadeo
 mercadeo mercadeo
mercadeo
 
S_Dinakaran_RHCSA
S_Dinakaran_RHCSAS_Dinakaran_RHCSA
S_Dinakaran_RHCSA
 
Sc method1
Sc method1Sc method1
Sc method1
 
πληροφορικη Nefeli kathrine
πληροφορικη Nefeli kathrineπληροφορικη Nefeli kathrine
πληροφορικη Nefeli kathrine
 
Lapata rewa janpad nrega me hera pheri7
Lapata rewa janpad nrega me hera pheri7Lapata rewa janpad nrega me hera pheri7
Lapata rewa janpad nrega me hera pheri7
 
10-Living Without Accumulated Negativity
10-Living Without Accumulated Negativity10-Living Without Accumulated Negativity
10-Living Without Accumulated Negativity
 
Learning styles john faught
Learning styles john faughtLearning styles john faught
Learning styles john faught
 
Užívateľské testovanie a sledovanie užívateľov
Užívateľské testovanie a sledovanie užívateľovUžívateľské testovanie a sledovanie užívateľov
Užívateľské testovanie a sledovanie užívateľov
 
X tensive dpa - мониторинг станков с ЧПУ университетам.
X tensive dpa - мониторинг станков с ЧПУ университетам.X tensive dpa - мониторинг станков с ЧПУ университетам.
X tensive dpa - мониторинг станков с ЧПУ университетам.
 
PetroSync - Pre-Commissioning, Commissioning and Start-Up
PetroSync - Pre-Commissioning, Commissioning and Start-UpPetroSync - Pre-Commissioning, Commissioning and Start-Up
PetroSync - Pre-Commissioning, Commissioning and Start-Up
 
Илья Коган, Яндекс.Маркет
Илья Коган, Яндекс.МаркетИлья Коган, Яндекс.Маркет
Илья Коган, Яндекс.Маркет
 
Сергей Малышев Почта России eRetailForum2015
Сергей Малышев Почта России eRetailForum2015Сергей Малышев Почта России eRetailForum2015
Сергей Малышев Почта России eRetailForum2015
 
Back to Basic
Back to BasicBack to Basic
Back to Basic
 

Similar to Vic weekly learning_20160325

Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVCjeffz
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniterChun-Kai Wang
 
ASP.Net MVC Framework
ASP.Net MVC FrameworkASP.Net MVC Framework
ASP.Net MVC Framework國昭 張
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC
 
Introduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesIntroduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesJeff Chu
 
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...SernHao TV
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
Script with engine
Script with engineScript with engine
Script with engineWebrebuild
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文Guo Albert
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejsChi-wen Sun
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing江華 奚
 
在Windows azure平台上進行資料庫處理及架構設計
在Windows azure平台上進行資料庫處理及架構設計在Windows azure平台上進行資料庫處理及架構設計
在Windows azure平台上進行資料庫處理及架構設計Sky Chang
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4Duran Hsieh
 
Essential oracle security internal for dba
Essential oracle security internal for dbaEssential oracle security internal for dba
Essential oracle security internal for dbamaclean liu
 
How to ASP.NET MVC4
How to ASP.NET MVC4How to ASP.NET MVC4
How to ASP.NET MVC4Daniel Chou
 
运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践Li JianYe
 
W3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in ReactW3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in React美团点评技术团队
 

Similar to Vic weekly learning_20160325 (20)

Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniter
 
ASP.Net MVC Framework
ASP.Net MVC FrameworkASP.Net MVC Framework
ASP.Net MVC Framework
 
twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸twMVC#01 | ASP.NET MVC 的第一次親密接觸
twMVC#01 | ASP.NET MVC 的第一次親密接觸
 
Introduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesIntroduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 Features
 
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
CRUD 綜合運用
CRUD 綜合運用CRUD 綜合運用
CRUD 綜合運用
 
Script with engine
Script with engineScript with engine
Script with engine
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejs
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
 
在Windows azure平台上進行資料庫處理及架構設計
在Windows azure平台上進行資料庫處理及架構設計在Windows azure平台上進行資料庫處理及架構設計
在Windows azure平台上進行資料庫處理及架構設計
 
Hibernate
HibernateHibernate
Hibernate
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
 
Essential oracle security internal for dba
Essential oracle security internal for dbaEssential oracle security internal for dba
Essential oracle security internal for dba
 
Nightwatch101
Nightwatch101Nightwatch101
Nightwatch101
 
How to ASP.NET MVC4
How to ASP.NET MVC4How to ASP.NET MVC4
How to ASP.NET MVC4
 
运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践运维系统开发与Rails 3页面开发实践
运维系统开发与Rails 3页面开发实践
 
W3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in ReactW3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in React
 

More from LearningTech

More from LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 
Asp.net MVC DI
Asp.net MVC DIAsp.net MVC DI
Asp.net MVC DI
 

Vic weekly learning_20160325

  • 2. 大綱 • MVC – Controller to View – ViewData – ViewBag – TempData – View Model – Tuple
  • 3. Controller to View • ViewData – ViewDataDictionary字典型別 – Public class ViewDataDictionary : IDictionary<string,object>{} – 必須要有string做Key值 – 無法跨action做存取 – ViewData[“text”] = “Hello MVC”;
  • 4. Controller to View • ViewBag – 與ViewData雷同 – dynamic型別 – 物件.屬性 – 在Action呼叫view page時自動傳遞過去 Action Public ActionResult demo() { ViewBag.items = _dc.abc.ToList(); Return View(); } View @foreach(var item in ViewData[“items”] )
  • 5. Controller to View • TempData – Action傳Action的時候 可以使用 – 亦是繼承與實作IDictionary – 使用session做資料傳遞 – 透過RedirectToAction導頁到不同的Action時,則可以取得到上一個Action所存放 在TempData的資料 – 在一個要求流程中被取出就會被刪除
  • 6. Controller to View • TempData 他是從ASP.NET執行環境一個名稱為 TempData的暫存資料中取資料而非 TempDataDictionary 因為在Source Code中會先判斷若是 子action的方法會直接從TempData 抓取資料而不會刪除session的資料 如果不希望從TempDataDictionary 取資料會被刪除的話使用keep()
  • 7. Controller to View • View Model – 大型專案往往需要同時投遞多個Model與物件 – 一個View 只能接收一個model陳述式 – ViewModel包含的屬性可以用來呈現(label for、editor for…) – View Model 可以包含多個不同的model 多個實體或物件
  • 8. Controller to View • Tuple – 省下建置ViewModel的時間 – 在View Page亦是強行別 – 可讀性低
  • 9. 強弱型別的差異 • 弱型別 – 沒有型別檢查 – 沒有IntelliSense – 編譯時期無法除錯 – Run time 才能確認是否無誤 – 執行實情才能得知型別資訊 • 強行別 – 編譯時期即能發現錯誤 – 變數能夠被IntelliSense支援 – 執行的速度也較快
  • 10. Reference • MVC5 網站開發美學 • http://techfunda.com/howto/asp-net-mvc
  • 11. Thank you for your listening

Editor's Notes

  1. 會忽略編譯時期的靜態型別檢查
  2. 會忽略編譯時期的靜態型別檢查
  3. 會忽略編譯時期的靜態型別檢查