The ViewData and TempData attributes are used to pass objects to the View page.
As mentioned above, ViewData can be used to transmit data from the Controller to the View.
In the previous article, we created the EiceController class
In the example in this article, we will change this Controller Namespace MvcApplication2.Controllers
{
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
/** // <Summary>
/// Do not remember what I mentioned earlier. All controllers must inherit from
/// Controller class. Of course, there are many Controller types.
/// </Summary>
Public class EiceController: Controller
{
Public void Index (){
ViewData ["ViewData"] = "ViewData is displayed here ";
TempData ["TempData"] = "show TempData" here ";
RenderView ("Index ");
}
Public void Index2 (){
RenderView ("Index ");
}
}
}
We remove the Index parameter and provide values for ViewData and TempData.
In View/Eice/Index. aspx, write the following code: 1: <% = ViewData ["ViewData"] %> <br/>
2: <% = TempData ["TempData"] %>
Note that the above 1.2 is not the row number...
Next we will run the project
Access http: // localhost/Eice/Index
You can see the following:
1. ViewData is displayed here
2. Show TempData here
Access http: // localhost/Eice/Index2 again
The result is 1.
2. Show TempData here
Here 1 shows the content in ViewData, and 2 shows the content passed by TempData.
We can see that
ViewData is valid only in this Action.
However, TempData can still exist on other pages like Session, but only one page of access is allowed (similar to Flash in Monorail)
TempData is generally used for temporary cache content or passing error messages when an error page is thrown.
The Redirect method redirects the page to other Controller/Action
RedirectToAction (Action name );
RedirectToAction (Action name, Controller name );
RedirectToAction (RouteValueDictionary );
There is nothing to say About RedirectToAction ("About", "Home") here.
The third type of Overload
You can write
System. Web. Routing. RouteData routeData = new System. Web. Routing. RouteData ();
RouteData. Values. Add ("Action", "About ");
RouteData. Values. Add ("Controller", "Home ");
RedirectToAction (routeData. Values );
In this way, the page Jump can be completed.
Of course, you can also use traditional Response. Redirect to Redirect pages.
Asp.net Mvc Framework Series