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?
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?
On
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
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:
Then in your controller, you can do:
HTH