iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 11
0
Modern Web

ASP.NET MVC5架構說明與Q&A諮詢系統實作系列 第 11

[DAY 11] Controller 基本程式碼介紹

以先前的 HomeController , HomeController 的 IndexAbout 為例子


  • HomeController 程式碼中的 Index
    https://ithelp.ithome.com.tw/upload/images/20200924/20128969f7jL93s0Rf.png

    當函式回傳值型別為 ActionResult,回傳值即為 return View()
    當程式執行到 View() 時,就會跑到 View結構中與 Action 對應的相同名稱 View/Home/Index 這裡,並執行此 Index 內的程式碼
    https://ithelp.ithome.com.tw/upload/images/20200924/20128969bpl09J7e1R.png

  • HomeController 程式碼中的 About
    這邊使用 ViewBag 方法來傳資料
    ViewBag 是 dynamic object(動態物件)型別,引用 System.Web.Mvc.ControllerBase.ViewBag,使用上較方便、不用特意轉型
    補充:ViewData 是 object(靜態物件) 型別,引用System.Web.Mvc.TempDataDictionary,必須透過轉型成強型別的方式,才能正確抓取資料
    https://ithelp.ithome.com.tw/upload/images/20200924/201289692JVAVofUkl.png

    這個一樣,當函式回傳值型別為 ActionResult,回傳值即為 return View()
    當程式執行到 View() 時,就會跑到 View/Home/About 這裡,並執行此 About 內的程式碼
    https://ithelp.ithome.com.tw/upload/images/20200924/20128969Ni0bOTBUZ0.png

  • Controller 與 Entity Framework
    以 QuerController 為例,new 出先前的 TestEntity 以抓取資料
    https://ithelp.ithome.com.tw/upload/images/20200925/201289697kmo69bM3g.png

    這裡有使用到一些 LINQ 的語法,明天會來介紹什麼是 LINQ (・ิω・ิ)
    https://ithelp.ithome.com.tw/upload/images/20200925/20128969P8W6ahY8M7.png


上一篇
[DAY 10] 使用 Scaffold 建 Controller、View
下一篇
[DAY 12] LINQ 是什麼?
系列文
ASP.NET MVC5架構說明與Q&A諮詢系統實作30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言