1. TempData is meant to be a very short-lived instance, and you should only use it
during the current and the subsequent requests only.
2. 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.
3. Therefore, the only scenario where using TempData will reliably work is when
you are redirecting.This is because a redirect kills the current request , then creates a
new request on the server to serve the redirected view.
4. Simply said, Asp.Net MVC TempData dictionary is used to share data between
controller actions.
I have written a article about this :
http://sampathloku.blogspot.com/2012/09/how-to-use-aspnet-mvc-tempdata-properly.html[
^]
So don't use TempData for carrign data from controller to View.For that you can use
ViewBag.
Controller
public ActionResult Index()
{
ViewBag.Name = "Hajan";
ViewBag.Age = 25;
return View();
}
View
<p>
My name is
<b><%: ViewBag.Name %></b>,
<b><%: ViewBag.Age %></b> years old.
<br />
I like the following colors:
</p>
More info :
http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx[
^]
I hope this will help to you.