Can somebody please explain the purpose of TempData in MVC. I understand it behaves like ViewBag but what does it do beyond that.
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.
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.
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.
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.
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
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";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With