Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

c# - Null TempData when passing data from controller to view MVC

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...