从控制器传递数据以查看MVC时,Null TempData [英] Null TempData when passing data from controller to view MVC

查看:138
本文介绍了从控制器传递数据以查看MVC时,Null TempData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Controller中将以下类传递数据到View:

I have the following class in a Controller passing data to a View:

public ActionResult ControllerToView(){
    ...
    TempData["example"] = "this is a message!";
    ...
    return Redirect("http://myViewPageLink");
}

在我的视图中,我试图通过以下方式访问TempData字典:

In my View, I am trying to access the TempData dictionary with:

@if(myCondition){
    var test = TempData["example"];
    <p>@test</p>
}

始终满足"myCondition",但TempData词典始终为空.有什么想法吗?为了使TempData在视图中可用,我是否需要编写其他代码?

"myCondition" is always satisfied, but the TempData dictionary is always empty. Any ideas why? Is there any aditional code I have to write in order to make TempData available in the view?

可能有用的信息是,在调用我的控制器方法之前,我向同一控制器中的另一个方法发出了ajax请求.

It might be useful information that before calling my controller method I have an ajax request to another method in the same controller.

推荐答案

您应该知道

You should know that TempDataDictionary used for short-term instance. Its value available during current & subsequent request when the next request surely redirects to next view (suitable for one-time messages). Any value you've assigned to TempDataDictionary will be discarded after completion of subsequent request, as "normal read".

以便您当前的请求包含以下顺序:

So that your current request consists of this sequence:

  1. 请求=> ActionResult(ControllerToView)
  2. 设置TempDataDictionary
  3. 响应=> RedirectResult
  4. Request => ViewResult ==>如果没有用于持久化数据的KeepPeek方法,此处的TempDataDictionary内容可能会被丢弃
  5. 响应=>视图(TempDataDictionary为空)
  1. Request => ActionResult (ControllerToView)
  2. Set TempDataDictionary
  3. Response => RedirectResult
  4. Request => ViewResult ==> the TempDataDictionary content may discarded here if no Keep or Peek method used to persist data
  5. Response => View (with TempDataDictionary is empty)

因此,正确使用TempDataDictionary的方法是直接传递值以在当前请求中查看,或使用重定向到另一个控制器操作方法作为后续请求,例如本例:

Hence the correct way to use TempDataDictionary is passing value directly to view in current request or using redirect to another controller action method as subsequent request, as this example:

控制器

public ActionResult ControllerToView()
{
    ...
    TempData["example"] = "this is a message!";
    ...
    // returning view counts as providing response
    return View();
}

查看

@if (myCondition)
{
    var test = TempData["example"]; // showing message
    <p>@test</p>
}

以下示例的请求顺序如下:

The request sequence for above example is given below:

  1. 请求=> ActionResult(ControllerToView)
  2. 设置TempDataDictionary
  3. 响应=>查看(TempDataDictionary不为空)
  1. Request => ActionResult (ControllerToView)
  2. Set TempDataDictionary
  3. Response => View (TempDataDictionary is not empty)

如果您使用RedirectResult,然后尝试在未指定'next action'的情况下在TempData中读取/显示值,则将其视为正常读取".不能持久用于下一个请求.您可以使用下一个动作":KeepPeek(在视图或控制器动作中):

If you use RedirectResult then trying to read/display value in TempData without specifying the 'next action', it considered as "normal read" & not persisted for next request. The 'next action' you can use: Keep or Peek (either in view or controller action):

// Keep
var test = TempData["example"];
TempData.Keep("example");

// Peek
var test = TempData.Peek("example");

注意:如果您希望设置值在多个请求中保持不变,我强烈建议

NB: If you want setting values to be persist across multiple requests, I strongly prefer HttpSessionState:

// set session state
Session["example"] = "[any value]";

// read in another request
var testing = Session["example"];

参考文献:

在ASP.NET MVC中使用Tempdata-最佳做法

何时使用ViewBag,ViewData或TempData

这篇关于从控制器传递数据以查看MVC时,Null TempData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆