TempData[]是一个可以跨Action的传递,且只传递一次.

TempData保存在Session中,Controller每次执行请求的时候,会从Session中先获取TempData,而后清除 Session,获取完TempData数据,虽然保存在内部字典对象中,但是其集合中的每个条目访问一次后就从字典表中删除。具体代码层 面,TempData获取过程是通过SessionStateTempDataProvider.LoadTempData方法从 ControllerContext的Session中读取数据,而后清除Session,故TempData只能跨Controller传递一次。

   1.在HomeController.cs中创建字典:

    public ActionResult Index()
        {
            TempData["str"] = "TempData传递一次字符串";
            Response.Redirect("/home/about");//跳转到about页中
            return View();
        }

MVC3.0入门学习笔记-页面传值-TempData

2.因为页面将跳转到about页,并希望该页接受到传值,所以定义应在About.cshtml中:

@TempData["strValue"];

MVC3.0入门学习笔记-页面传值-TempData

我们已经可以看到我们传递的值了,但是刷新以后,将不会显示,因为该传值只能传递一次

MVC3.0入门学习笔记-页面传值-TempData

相关文章:

  • 2022-12-23
  • 2021-03-31
  • 2021-10-01
  • 2021-12-08
  • 2021-08-14
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2021-07-22
  • 2021-12-24
  • 2022-12-23
相关资源
相似解决方案