//---------------------------------控制器向视图传递数据 public ActionResult TransData() { //1.ViewBag ViewBag.Hello = "hello world~~~"; //2.ViewData ViewData["Think"] = "C#"; //3.TempData TempData["TempHH"] = "我是tempda"; //4.Model //Model属性,本质上就是ViewData.Model Models.Student entity = new Student() { s_Name = "传智播客视频学习" }; ViewData.Model = entity; return View(); }
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>TransData</title> </head> <body> <div> @ViewBag.Hello<br/> @ViewData["Think"]<br /> @TempData["TempHH"]<br /> @Model.s_Name @ViewData.Model.s_Name @*在视图上,ViewBag共享ViewData的数据,同样ViewData共享ViewBag的数据*@ @ViewBag.Think @ViewData["Hello"] @ViewData["TempHH"] @*//在页面上显示不出来,可以看出,TempData和ViewData以及ViewBag之间不共享数据*@ @ViewBag.TempHH </div> </body> </html>
总结:TempData可以在控制器的Action之间传递数据。ViewDate和ViewBag都不行。这个是自己学习的时候发现的
TempData和ViewData以及ViewBag之间不共享数据*@