.NET Mvc中ViewBag、ViewData、TempData如何使用

ViewBag   获取动态视图数据字典      作用:给视图传递数据,不需要转换类型,由系统动态解析,比ViewData执行性能要差

ViewData    获取或设置视图数据的字典            给视图传递数据,需要转换成相应的类型,编写没有ViewBag方便,可读性强

TempData     临时数据的字典             给控制器或视图传递数据,需要和ViewData一样进行类型转换,可以在多个控制器或页面传值,但是只能读取一次就为null了

 

后台代码

 1 //
 2         // GET: /CommonStudy/
 3 
 4         public ActionResult Index()
 5         {
 6             var model = Sxy.BLL.CradInfo.GetList("", "");
 7 
 8             this.ViewBag.SecondModelList = model.ToList();
 9             //自定义数据
10             this.ViewData["ThirdModel"] = model.FirstOrDefault();
11             //缓存,在下一个控制器可以取到当前的数据,但是只能去一次就释放了
12             this.TempData["IndexCache"] = model.ToList();
13 
14             //只能被取一次,不管是视图使用还是控制器使用,只要其中一个使用了,当前临时缓存就为null了
15             this.TempData["Temp"] = "设置一个缓存";
16 
17             return View(model);
18         }
19 
20 
21         //
22         // GET: /CommonStudy/Create
23 
24         public ActionResult Create()
25         {
26             //获取IndexCache
27             var tempData = this.TempData["IndexCache"];
28             //转换成实例象
29             var model = tempData as List<Sxy.Model.CradInfo>;
30             var tempData2 = this.TempData["IndexCache"];
31             //在当前控制存储,给后面一个控制器使用
32             this.TempData["CreateCache"] = tempData2;
33 
34             //由于前面视图有使用到了这个临时缓存,现在已取不到了
35             var temp = this.TempData["Temp"];
36 
37             return View();
38         }
39 
40   //
41         // POST: /CommonStudy/Create
42 
43         [HttpPost]
44         public ActionResult Create(Sxy.Model.CradInfo model)
45         {
46             try
47             {
48                 // TODO: Add insert logic here
49                 var tempData2 = this.TempData["IndexCache"];
50                 var createCache = this.TempData["CreateCache"];
51                 if (ModelState.IsValid)
52                 {
53                     if (Sxy.BLL.CradInfo.Add(model) > 0)
54                     {
55 
56                     }
57                 }
58 
59 
60                 return RedirectToAction("Index");
61             }
62             catch
63             {
64                 return View();
65             }
66         }
View Code

前台代码

  1 @model IEnumerable<Sxy.Model.CradInfo>
  2 
  3 @{
  4     ViewBag.Title = "Index";
  5 }
  6 
  7 <h2>Index</h2>
  8 
  9 <p>
 10     @Html.ActionLink("Create New", "Create")
 11     @*临时数据*@
 12     @this.TempData["Temp"]
 13 
 14 </p>
 15 <table>
 16     <tr>
 17         <th>
 18             @Html.DisplayNameFor(model => model.userId)
 19         </th>
 20         <th>
 21             @Html.DisplayNameFor(model => model.carNumber)
 22         </th>
 23         <th>
 24             @Html.DisplayNameFor(model => model.carQueryPassword)
 25         </th>
 26         <th>
 27             @Html.DisplayNameFor(model => model.createTime)
 28         </th>
 29         <th></th>
 30     </tr>
 31     @*默认返回的Model*@
 32     @foreach (var item in Model)
 33     {
 34         <tr>
 35             <td>
 36                 @Html.DisplayFor(modelItem => item.userId)
 37             </td>
 38             <td>
 39                 @Html.DisplayFor(modelItem => item.carNumber)
 40             </td>
 41             <td>
 42                 @Html.DisplayFor(modelItem => item.carQueryPassword)
 43             </td>
 44             <td>
 45                 @Html.DisplayFor(modelItem => item.createTime)
 46             </td>
 47             <td>
 48             @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
 49             @Html.ActionLink("Details", "Details", new { id = item.id }) |
 50             @Html.ActionLink("Delete", "Delete", new { id = item.id })
 51             </td>
 52         </tr>
 53     }
 54 
 55     <tr>
 56         <td colspan="5">
 57             <hr />
 58         </td>
 59 
 60     </tr>
 61     @*自定义ViewData*@
 62     @foreach (var item in ViewData["SecondModelList"] as List<Sxy.Model.CradInfo>)
 63     {
 64         <tr>
 65             <td>
 66                 @Html.DisplayFor(modelItem => item.userId)
 67             </td>
 68             <td>
 69                 @Html.DisplayFor(modelItem => item.carNumber)
 70             </td>
 71             <td>
 72                 @Html.DisplayFor(modelItem => item.carQueryPassword)
 73             </td>
 74             <td>
 75                 @Html.DisplayFor(modelItem => item.createTime)
 76             </td>
 77             <td>
 78                 @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
 79             @Html.ActionLink("Details", "Details", new { id = item.id }) |
 80             @Html.ActionLink("Delete", "Delete", new { id = item.id })
 81             </td>
 82         </tr>
 83     }
 84 
 85     <tr>
 86         <td style="width: auto">
 87             <hr />
 88         </td>
 89 
 90     </tr>
 91 
 92 
 93     @{ var ThirdModel = ViewData["ThirdModel"] as Sxy.Model.CradInfo;
 94         <tr>
 95 
 96             <td>
 97                 @Html.DisplayFor(modelItem => ThirdModel.userId)
 98             </td>
 99             <td>
100                 @Html.DisplayFor(modelItem => ThirdModel.carNumber)
101             </td>
102             <td>
103                 @Html.DisplayFor(modelItem => ThirdModel.carQueryPassword)
104             </td>
105             <td>
106                 @Html.DisplayFor(modelItem => ThirdModel.createTime)
107             </td>
108             <td>
109                 @Html.ActionLink("Edit", "Edit", new { id = ThirdModel.id }) |
110             @Html.ActionLink("Details", "Details", new { id = ThirdModel.id }) |
111             @Html.ActionLink("Delete", "Delete", new { id = ThirdModel.id })
112             </td>
113         </tr>
114         
115     }
116 
117 
118 
119 </table>
View Code

 

posted @ 2015-11-19 19:48  雪~中~狼  阅读(462)  评论(0编辑  收藏  举报