This series of articles is based on ASP. net mvc Preview5.
ASP. net mvc TempData is used to transmit some temporary data, such as transferring temporary data between various controller actions or passing some temporary data to the View. which of the following methods can be used to pass values between pages. in net mvc, TempData is one of the value passing methods. By default, TempData uses sessions to store temporary data. The data stored in TempData is valid only once and is deleted once. This access refers to a request to the next request, because after the next request arrives, the TempData data stored in the Session will be retrieved from the Session and assigned to TempData, then, the data is deleted from the Session. Let's take a look at ASP. net mvc Preview5 source code:
That is to say, TempData is saved only to the next request. After the next request is complete, TempData will be deleted. Note that TempData uses sessions for storage. Sessions correspond to specific users, so there is no concurrency problem. If you use a database as the storage medium for TempData, you must consider this situation. As for how to customize the storage medium of TempData, you can refer to the article "ASP. net mvc: Use db4o for TempDataProvider (a generic RedirectToAction method is attached.
As mentioned above, there is a method in our BaseController to display the prompt information to the user. This prompt information is temporary information, which can be implemented using TempData. The following describes how to implement the prompt information:
Protected ActionResult ShowMsg (List <string> msgs)
{
TempData ["Messages"] = msgs;
Return RedirectToAction ("Message ");
}
Public ActionResult Message ()
{
Return View (TempData ["Messages"] as List <string> );
}
Because our controllers all inherit from our custom BaseController, We can display the prompt information in the Controller as follows: