What is the difference between Session and TempData in MVC?

 Posted by Kmandapalli on 4/18/2013 | Category: ASP.NET MVC Interview questions | Views: 67586 | Points: 40
Select from following answers:
  1. No difference, both of them are same.
  2. We cannot use Sessions in MVC.
  3. TempData is restricted to a controller whereas Session is not.
  4. Sessions are restricted to a controller whereas TempData is not
  5. All Above

Show Correct Answer


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Vijayabhaskarsc1 on: 8/3/2013 | Points: 10
Mentioned answer is wrong.
TempData object can be used across the controllers
Please follow below steps
public ActionResult Create(Movie movie)
{
@TempData["sData"] = "UserName";
return RedirectToAction("Welcome","HelloWorld");
}

HelloworldController.cs
Welcome.cshtml

<h2>Welcome @TempData["sData"]</h2>
Posted by: Raghav1919 on: 11/25/2015 | Points: 10
The difference is once we used Tempdata not available for next use.

If you read the “TempData” and call the “Keep” method it will be persisted.

Ex;
@TempData[“Raghav”];
TempData.Keep(“Raghav”);

If you read “TempData” by using the “Peek” method it will persist for the next request.

Ex:
TestStr = TempData.Peek("Raghav").ToString();

Login to post response

More Interview Questions by Kmandapalli