cnblogs/dcrenl/TempData、ViewData和ViewBag异同.html
2024-09-24 12:43:01 +08:00

11 lines
1.0 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div id="sina_keyword_ad_area2" class="articalContent "> Data ViewData ViewBag都可以用来保存数据。
<div>它们之间的区别如下:</div>
<div><strong>TempData</strong>保存在Session中Controller每次执行请求的时候会从Session中先获取TempData而后清除Session获取完TempData数据虽然保存在内部字典对象中但是其集合中的每个条目访问一次后就从字典表中删除。具体代码层面TempData获取过程是通过SessionStateTempDataProvider.LoadTempData方法从ControllerContext的Session中读取数据而后清除Session故TempData只能跨Controller传递一次。</div>
<div>&nbsp;</div>
<div><strong>ViewData</strong>生命周期和View相同仅对当前View有效。</div>
<div>&nbsp;</div>
<div>
<strong>ViewBag</strong>和ViewData生命周期相同也是对但前View有效不同的是ViewBag的类型不再是字典的键值对结构而是dynamic动态类型属于MVC3里面新增的部分。</div>
</div>
<p>&nbsp;</p>