从控制器传递数据以查看MVC时,Null TempData [英] Null TempData when passing data from controller to view MVC
问题描述
我在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:
- 请求=> ActionResult(ControllerToView)
- 设置
TempDataDictionary
- 响应=> RedirectResult
- Request => ViewResult ==>如果没有用于持久化数据的
Keep
或Peek
方法,此处的TempDataDictionary
内容可能会被丢弃 - 响应=>视图(
TempDataDictionary
为空)
- Request => ActionResult (ControllerToView)
- Set
TempDataDictionary
- Response => RedirectResult
- Request => ViewResult ==> the
TempDataDictionary
content may discarded here if noKeep
orPeek
method used to persist data - 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:
- 请求=> ActionResult(ControllerToView)
- 设置
TempDataDictionary
- 响应=>查看(
TempDataDictionary
不为空)
- Request => ActionResult (ControllerToView)
- Set
TempDataDictionary
- Response => View (
TempDataDictionary
is not empty)
如果您使用RedirectResult
,然后尝试在未指定'next action'的情况下在TempData
中读取/显示值,则将其视为正常读取".不能持久用于下一个请求.您可以使用下一个动作":Keep
或Peek
(在视图或控制器动作中):
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"];
参考文献:
这篇关于从控制器传递数据以查看MVC时,Null TempData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!