首页 > 代码库 > Mvc中ViewBag、ViewData、TempData如何使用

Mvc中ViewBag、ViewData、TempData如何使用

对于我初学者来说,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 = http://www.mamicode.com/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>