Null TempData when passing data from controller to view MVC

你离开我真会死。 提交于 2019-12-07 11:36:32

问题


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");
}

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

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

"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?

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


回答1:


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. 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)

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:

Controller

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

View

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

The request sequence for above example is given below:

  1. Request => ActionResult (ControllerToView)
  2. Set TempDataDictionary
  3. Response => View (TempDataDictionary is not empty)

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"];

References:

Using Tempdata in ASP.NET MVC - Best practice

When to use ViewBag, ViewData, or TempData



来源:https://stackoverflow.com/questions/45356403/null-tempdata-when-passing-data-from-controller-to-view-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!