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) > )
{ }
} 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>

最新文章

  1. [转]Centos安装zeromq和jzmq
  2. Intel系列CPU的流水线技术的发展
  3. tc 147 2 PeopleCircle(再见约瑟夫环)
  4. 7-14 EXISTS子查询
  5. springMVC简单示例
  6. tyvj 1055 区间dp
  7. SSH登录失败:Host key verification failed
  8. opengl笔记
  9. Codeforces 85D Sum of Medians(线段树)
  10. hdu 5094 Maze(水搜索)
  11. 10、手把手教你Extjs5(十)自定义模块的设计
  12. iOS开源库--最全的整理 分类: ios相关 2015-04-08 09:20 486人阅读 评论(0) 收藏
  13. js基本语法汇总
  14. MySQL高可用复制管理工具 —— Orchestrator使用
  15. https加密流程
  16. 解决SQLite异常:library routine called out of sequence
  17. Digispark kickstarter + JoyStick 模拟鼠标
  18. SSO集成方案[随笔]
  19. PAT B1034 有理数四则运算 (20 分)
  20. STM32F4: Generating parallel signals with the FSMC

热门文章

  1. js正则表达式校验非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
  2. Android Studio的SVN Performing VCS Refresh/Commit 长时间不结束
  3. java的原子性操作有哪些
  4. MySQL基础知识和操作(一)
  5. Linux归档压缩、分区管理与LVM管理
  6. Spring 4 + Quartz 2.2.1 Scheduler Integration Example
  7. JavaScript--面向对象--猜拳游戏
  8. Web Worker javascript多线程编程(一)
  9. AEAI ESB培训大纲
  10. webstorm官网最新版激活: