Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC C# TempData

Can somebody please explain the purpose of TempData in MVC. I understand it behaves like ViewBag but what does it do beyond that.

like image 514
Nate Pet Avatar asked May 07 '12 18:05

Nate Pet


People also ask

What is MVC in C?

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application.

What is the difference between MVC and C#?

They are the same thing. C# is the language you have used to do your development, but ASP.NET MVC is the framework you used to do it.

What MVC is used for?

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display.

What is controller in MVC C#?

Controllers are essentially the central unit of your ASP.NET MVC application. It is the 1st recipient, which interacts with incoming HTTP Request. So, the controller decides which model will be selected, and then it takes the data from the model and passes the same to the respective view, after that view is rendered.


3 Answers

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. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view. Looking back at the previous HomeController code sample means that the TempData object could yield results differently than expected because the next request origin can't be guaranteed. For example, the next request can originate from a completely different machine and browser instance.

http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

like image 133
Jakub Konecki Avatar answered Oct 20 '22 20:10

Jakub Konecki


ViewBag

Allows you to create dynamic properties

Passing data between the controller and view

Controller

ViewBag.Name= "Lion";

View

<div>
  <h4>@ViewBag.Name</h4>
 </div>

TempData

TempData is meant to be a very short-lived instance

you should only use it during the current and the subsequent requests only

TempData dictionary is used to share data between controller actions

TempData["Name"] = "Lion";
like image 40
Sampath Avatar answered Oct 20 '22 19:10

Sampath


TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.

public TempDataDictionary TempData { get; set; }

It is a property of ControllerBase class.It is used to pass data from current request to subsequent request (means redirecting from one page to another). It’s life is very short and lies only till the target view is fully loaded. It’s required typecasting for getting data and check for null values to avoid error.It is used to store only one time messages like error messages, validation messages.

like image 27
itzmebibin Avatar answered Oct 20 '22 20:10

itzmebibin