TempData Vs Session in ASP .NET MVC:
ππ»TempData
β TempData allow us to persisting data for the duration of single subsequent request.
β ASP. net MVC will automatically expire the value of tempdata once consecutive request returned the result (it means, it alive only till the target view is fully loaded).
β It valid for only current and subsequent request only
β TempData has Keep method to retention the value of TempData.
Example:
TempData.Keep(), TempData.Keep(βEmpNameβ)
β TempData internally stored the value in to Session variable.
β It is used to stored only one time messages like validation messages, error messages etc.
ππ»Session:
β‘οΈSession is able to store data much more long time, until user session is not expire.
β‘οΈSession will be expire after the session time out occurred.
β‘οΈIt valid for all requests.
β‘οΈN/A
β‘οΈSession variable are stored in SessionStateItemCollection object (Which is exposed through the HttpContext.Session property of page).
β‘οΈIt is used to stored long life data like user id, role id etc. which required throughout user session.
β‘οΈTempData and session, both required typecasting for getting data and check for null values to avoid run time exception.
ππ»TempData
β TempData allow us to persisting data for the duration of single subsequent request.
β ASP. net MVC will automatically expire the value of tempdata once consecutive request returned the result (it means, it alive only till the target view is fully loaded).
β It valid for only current and subsequent request only
β TempData has Keep method to retention the value of TempData.
Example:
TempData.Keep(), TempData.Keep(βEmpNameβ)
β TempData internally stored the value in to Session variable.
β It is used to stored only one time messages like validation messages, error messages etc.
ππ»Session:
β‘οΈSession is able to store data much more long time, until user session is not expire.
β‘οΈSession will be expire after the session time out occurred.
β‘οΈIt valid for all requests.
β‘οΈN/A
β‘οΈSession variable are stored in SessionStateItemCollection object (Which is exposed through the HttpContext.Session property of page).
β‘οΈIt is used to stored long life data like user id, role id etc. which required throughout user session.
β‘οΈTempData and session, both required typecasting for getting data and check for null values to avoid run time exception.