MVC Controller 與View 傳值
剛從資策會畢業,第一個專案就是ASP MVC,所以整理了一下,MVC幾個常用的Controller的View的傳值的方法~
Controller |
View |
ViewData["currentTime"] = DateTime.Now; |
ViewData : @ViewData["currentTime"] |
TempData["currentTime"] = DateTime.Now; (可跨 Action) |
TempData : @TempData["currentTime"] |
ViewBag.currentTime = DateTime.Now.AddDays(7); |
ViewBag : @ViewBag.currentTime |
Session["currentTime"] = DateTime.Now; (可跨 Action) |
Session : @Session["currentTime"] |
Response.Cookies["theName"].Value = "Tom"; |
@if (Request.Cookies["theName"] != null) { Cookies : @Request.Cookies["theName"].Value } |
將Model傳入View有以下兩種 1.ViewData.Model = db.Categories.ToList(); 2.return View(db.Categories.ToList()); |
@model IEnumerable<Categories> |