Passing data from controller to the view
ViewData
: 是一個 key value 的 dictionary, key 的 data type 規定是 string, ViewData 的 life time value 只會存在這一個 current requestViewBag
: 是一個 dynamic typed, 可以裝任何的 typed using set method, 也可以直接 get 裡面所裝的 object, life time 的 value 只會存在這一個 current requestTempData
: 跟ViewData
一樣是 key-value dictoinary, 但是 life time value 會持續到 next request, 比較常用的是在 redirect 時要記得 model 的 state
使用方法: 可以把
ViewBag
的 key 叫做 Product, 並且 assign 一個 product object 給 ViewBag
, 這裡只是使用方便, 所以直接 hardcode product, 一般的狀況要用 LINQ
去 database 拿資料, 並且在 assign
最後在
Views/Details.cshtml
來拿到這一個 ViewBag
裡面的東西
留言
張貼留言