你的位置:首页 > ASP.net教程

[ASP.net教程]MVC ,Action方法传数据给视图有几种方式?


 //---------------------------------控制器向视图传递数据    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>