TempData value not persisting if used in view
Asked Answered
D

4

6

I am using

TempData["hdn"] = "1";

in controller

If I use this

 @{
      var hdn = (string)TempData["hdn"];
  }

in View, TempData["hdn"] value in getting null in POST. If I skip this code in view it persists in POST. Why this is happening?

Douville answered 28/8, 2013 at 11:53 Comment(2)
How often do you access TempData? TempData is used to persist things between calls, but is cleared once accessed. (You may want to either pass the value in with a query parameter or your form submission). (See squaredroot.com/2007/12/20/mvc-viewdata-vs-tempdata)Lanta
You should not use Form Values. You could select suggestion given by SatpalEctoenzyme
K
10

TempData values are cleared after they are read.

if you want the value back in the controller after you have read it in the view, then you will need to include it in a hidden field and then read it out from the form values.

something like:

<input type="hidden" name="hdn" value="@hdn" />

Then in your controller, you can do:

var hdn = Request.Form["hdn"]

HTH

Km answered 28/8, 2013 at 12:6 Comment(1)
And here's a pointer to more info on passing data in MVC: msdn.microsoft.com/en-us/library/dd394711%28v=vs.100%29.aspxUnwept
T
10

TempData is like ViewData but with a difference. It can contain data between two successive requests, after that they are destroyed.

If you want to keep TempData value the use

TempData.Keep()

Example:

var hdn= TempData["hdn"]; //it is marked for deletion
TempData.Keep("hdn"); //unmarked it

MSDN Docs for Keep

Tamatave answered 28/8, 2013 at 12:5 Comment(0)
K
10

TempData values are cleared after they are read.

if you want the value back in the controller after you have read it in the view, then you will need to include it in a hidden field and then read it out from the form values.

something like:

<input type="hidden" name="hdn" value="@hdn" />

Then in your controller, you can do:

var hdn = Request.Form["hdn"]

HTH

Km answered 28/8, 2013 at 12:6 Comment(1)
And here's a pointer to more info on passing data in MVC: msdn.microsoft.com/en-us/library/dd394711%28v=vs.100%29.aspxUnwept
B
5

A TempData key & value set will be deleted after it has been called. Satpal talked about Keep, but you can also use Peek if you want to be explicit about every time you want to retrieve it without having it deleted.

TempData.Peek(String)

Example:

var hdnNotDeleted = TempData.Peek["hdn"];

MSDN Documentation for Peek

Brinkmanship answered 2/11, 2015 at 10:55 Comment(0)
Z
1

If your controller action returns a ViewResult, and you are tempted to put data into TempData, Don’t do That.Use ViewData/ViewBag, instead, in this case. TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only. Since TempData works this way, you need to know for sure what the next request will be, and Redirecting to another View is the only time you can guarantee this. Therefore, the only scenario where using TempData will Reliably work is when you are Redirecting. So Keep in Mind.

The best ever explanation: http://sampathloku.blogspot.com/2012/09/how-to-use-aspnet-mvc-tempdata-properly.html

Zachariah answered 12/7, 2019 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.