標籤:style blog http color 使用 os io 資料
對於我初學者來說,Mvc有很多都是我所迷惑的,也是我瞭解後所癡迷的。廢話不多說,來談談我今天對Mvc這幾個常用的對象的理解吧,這裡面只簡明概要敘述
ViewBag 擷取動態視圖資料字典 作用:給視圖傳遞資料,不需要轉換類型,由系統動態解析,比ViewData執行效能要差
ViewData 擷取或設定視圖資料的字典 給視圖傳遞資料,需要轉換成相應的類型,編寫沒有ViewBag方便,可讀性強
TempData 臨時資料的字典 給控制器或視圖傳遞資料,需要和ViewData一樣進行類型轉換,可以在多個控制器或頁面傳值,但是只能讀取一次就為null了
後台代碼
// // GET: /CommonStudy/ public ActionResult Index() { var model = Sxy.BLL.CradInfo.GetList("", ""); this.ViewBag.SecondModelList = model.ToList(); //自訂資料 this.ViewData["ThirdModel"] = model.FirstOrDefault(); //緩衝,在下一個控制器可以取到當前的資料,但是只能去一次就釋放了 this.TempData["IndexCache"] = model.ToList(); //只能被取一次,不管是視圖使用還是控制器使用,只要其中一個使用了,當前臨時緩衝就為null了 this.TempData["Temp"] = "設定一個緩衝"; return View(model); } // // GET: /CommonStudy/Create public ActionResult Create() { //擷取IndexCache var tempData = this.TempData["IndexCache"]; //轉換成執行個體象 var model = tempData as List<Sxy.Model.CradInfo>; var tempData2 = this.TempData["IndexCache"]; //在當前控制儲存,給後面一個控制器使用 this.TempData["CreateCache"] = tempData2; //由於前面視圖有使用到了這個臨時緩衝,現在已取不到了 var temp = this.TempData["Temp"]; return View(); } // // POST: /CommonStudy/Create [HttpPost] public ActionResult Create(Sxy.Model.CradInfo model) { try { // TODO: Add insert logic here var tempData2 = this.TempData["IndexCache"]; var createCache = this.TempData["CreateCache"]; if (ModelState.IsValid) { if (Sxy.BLL.CradInfo.Add(model) > 0) { } } return RedirectToAction("Index"); } catch { return View(); } }
前台代碼
@model IEnumerable<Sxy.Model.CradInfo>@{ ViewBag.Title = "Index";}<h2>Index</h2><p> @Html.ActionLink("Create New", "Create") @*臨時資料*@ @this.TempData["Temp"]</p><table> <tr> <th> @Html.DisplayNameFor(model => model.userId) </th> <th> @Html.DisplayNameFor(model => model.carNumber) </th> <th> @Html.DisplayNameFor(model => model.carQueryPassword) </th> <th> @Html.DisplayNameFor(model => model.createTime) </th> <th></th> </tr> @*預設返回的Model*@ @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.userId) </td> <td> @Html.DisplayFor(modelItem => item.carNumber) </td> <td> @Html.DisplayFor(modelItem => item.carQueryPassword) </td> <td> @Html.DisplayFor(modelItem => item.createTime) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.id }) | @Html.ActionLink("Details", "Details", new { id = item.id }) | @Html.ActionLink("Delete", "Delete", new { id = item.id }) </td> </tr> } <tr> <td colspan="5"> <hr /> </td> </tr> @*自訂ViewData*@ @foreach (var item in ViewData["SecondModelList"] as List<Sxy.Model.CradInfo>) { <tr> <td> @Html.DisplayFor(modelItem => item.userId) </td> <td> @Html.DisplayFor(modelItem => item.carNumber) </td> <td> @Html.DisplayFor(modelItem => item.carQueryPassword) </td> <td> @Html.DisplayFor(modelItem => item.createTime) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.id }) | @Html.ActionLink("Details", "Details", new { id = item.id }) | @Html.ActionLink("Delete", "Delete", new { id = item.id }) </td> </tr> } <tr> <td style="width: auto"> <hr /> </td> </tr> @{ var ThirdModel = ViewData["ThirdModel"] as Sxy.Model.CradInfo; <tr> <td> @Html.DisplayFor(modelItem => ThirdModel.userId) </td> <td> @Html.DisplayFor(modelItem => ThirdModel.carNumber) </td> <td> @Html.DisplayFor(modelItem => ThirdModel.carQueryPassword) </td> <td> @Html.DisplayFor(modelItem => ThirdModel.createTime) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = ThirdModel.id }) | @Html.ActionLink("Details", "Details", new { id = ThirdModel.id }) | @Html.ActionLink("Delete", "Delete", new { id = ThirdModel.id }) </td> </tr> }</table>