您的位置:首页 > 其它

MVC3中viewdata,tempdata,viewbag总结

2013-08-24 23:43 204 查看
MVC3中的Tempdata,viewdata,viewbag的区别与联系

------------------------------------------------------------------------------------------------------------------

名词解释:

1、ViewData

ViewData的生命周期和View相同, 只对当前View有效。
2、TempData

TempData保存在Session中, Controller每次执行请求的时候会从Session中获取TempData并删除Session。
获取完TempData数据后虽然保存在内部的字典对象中。
但是TempData集合的每个条目访问一次后就从字典表中删除。
也就是说TempData的数据至多只能经过一次Controller传递。
并且每个元素至多只能访问一次。
 

3、ViewBag

ViewBag 是 MVC3 新增语法,ViewBag 不再是字典的键值对结构,而是 dynamic 动态类型,它会在程序运行的时候动态解析。
----------------------------------------------------------------------------------------------------------------

下面我来讲讲他们之间的区别:

ViewData和TempData是字典类型,赋值方式用字典方式, ViewData["myName"]
ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.myName。
ViewBag和ViewData只在当前Action中有效,等同于View
TempData可以通过转向继续使用,因为它的值保存在Session中。
但TempData只能经过一次传递,之后会被系统自动清除.
ViewData和ViewBag 中的值可以互相访问,因为ViewBag的实现中包含了ViewData
他们之间的细节比较:

1.ViewData和ViewBag

ViewBag和ViewData生命周期相同,仅对当前View有效,不同的是ViewBag的类型不再是字典的键值对结构,而是dynamic动态类型。

 

2.TempData

TempData保存在Session中,Controller每次执行请求的时候,会从Session中先获取TempData,而后清除Session,获取完TempData数据,虽然保存在内部字典对象中,但是其集合中的每个条目访问一次后就从字典表中删除。

具体代码层面,TempData获取过程是通过SessionStateTempDataProvider.LoadTempData方法从ControllerContext的Session中读取数据,而后清除Session,故TempData只能跨Controller传递一次。

如果我们想读取TempData的值但是又不让它被删除,可以使用TempData.Peek("Key")方法。如果想再保持一次TempData里面的值,可以使用TempData.Keep("Key").

 

实例讲解:

虽然ViewData和TempData都可以传递弱类型数据,但是两者的使用是有区别的:

ViewData的生命周期和View相同, 只对当前View有效.TempData保存在Session中, Controller每次执行请求的时候会从Session中获取TempData并删除Session, 获取完TempData数据后虽然保存在内部的字典对象中,但是TempData集合的每个条目访问一次后就从字典表中删除. 也就是说TempData 的数据至多只能经过一次Controller传递.

示例1:首先我们会创建一个TestController控制器,控制器有如下三个方法,视图主要的工作还是输出TempData的值;

XHTML

public class TestController : Controller
{
//
// GET: /Test/

public ActionResult Index()
{
TempData["val"] = "hello";
return View();
}
public ActionResult One()
{
return View();
}
public ActionResult Two()
{
return View();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

public
class TestController
: Controller
    {
        //
        //
GET: /Test/
 
        public
ActionResult Index()
        {
            TempData["val"]
= "hello";
            return
View();
        }
        public
ActionResult One()
        {
            return
View();
        }
        public
ActionResult Two()
        {
            return
View();
        }
    }

在地址栏输入'../test/index',再次输入地址'../test/one'.我们可以在one视图中输出TempData['val']的值,但是我们再次在地址栏输入'../test/two',将会报错"未将对象引用到实例",说明TempData已经被销毁.

实例2:

XHTML

public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
TempData["val"] = "hello";
string val = TempData["val"].ToString();
//我们试着读取TempData的值之后,TempData将会被销毁
//但是这里有一个特殊的情况,并不是Tempdata访问后就会被销毁,
// 而是Action返回的类型是ActionResult的子类RidrectResult,
// RedirectToRouteResult类时,TempData不会在Action放回View后被销毁,我们
// 下节将会讲到
return View();
}
public ActionResult One()
{
return View();
}
public ActionResult Two()
{
return View();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

public
class TestController
: Controller
    {
        //
        //
GET: /Test/
        public
ActionResult Index()
        {
            TempData["val"]
= "hello";
            string
val =
TempData["val"].ToString();
            //我们试着读取TempData的值之后,TempData将会被销毁
            //但是这里有一个特殊的情况,并不是Tempdata访问后就会被销毁,
            //      而是Action返回的类型是ActionResult的子类RidrectResult,
            //      RedirectToRouteResult类时,TempData不会在Action放回View后被销毁,我们
            //      下节将会讲到
            return
View();
        }
        public
ActionResult One()
        {
            return
View();
        }
        public
ActionResult Two()
        {
            return
View();
        }
    }

当我们在地址栏输入'../test/index'后,我们再次在地址栏输入'../test/one'时,将会报错"未将对象引用到实例",说明TempData在已经在"string val =TempData["val"].ToString()时被销毁了.

实例3:

XHTML

public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
TempData["val"] = "hello";
string val = TempData["val"].ToString();
return RedirectToAction("One");
}
public ActionResult One()
{
return RedirectToAction("Two");
}
public ActionResult Two()
{
return View();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public
class TestController
: Controller
    {
        //
        //
GET: /Test/
        public
ActionResult Index()
        {
            TempData["val"]
= "hello";
            string
val =
TempData["val"].ToString();
            return
RedirectToAction("One");
        }
        public
ActionResult One()
        {
            return
RedirectToAction("Two");
        }
        public
ActionResult Two()
        {
            return
View();
        }

但我们输入'../test/index'是,我们从firebug中可以看出



可能有人会问不会报错吗?因为当我们读取TempData数据后,会自动销毁TempData.这点儿是没有错,但是mvc2有一个奇特的地方,mvc2.0只有将RedirectResult类或RedirectToRouteResult类当成ActionResult类时,才会强制保留TempData。除此之外,如果存在使用读取TempData的键值,默认就会在当次网页请求中将TempData清除.但是,如果只设定了TempData的值而没有读取的行为,TempData还会被保存到下一个页面中执行.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: