Mvc中ViewBag、ViewData、TempData如何使用

來源:互聯網
上載者:User

標籤: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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.