There are 6 methods for transferring values from an Action to a view, and 6 methods for transferring values from an action view.
When using ASP. net mvc often encounters the problem of passing values from Action to view during project development. Today I have summarized the methods I know and divided them into the following six types:
1. Use ViewData to transmit values
The following code is provided in the Action: ViewData ["name"] = "Vibin1 ";
You can receive the following message in the View: name: @ ViewData ["name"]
Note:ViewData is a simple dictionary with the same lifecycle as the requested View. It is only valid for the current View.
2. Use ViewBag to transmit values
The following code is provided in the Action: ViewBag. name = "Vibin2 ";
In the view, you can receive: name: @ ViewBag. name
Note:: The ViewBag above is essentially passed through ViewData, so when both ViewData and ViewBag are used, if the Defined variables are the same, the previous ones will be overwritten by the subsequent ones, for example, if you use either of the following methods: Name: Vibin2. If you want to use these two value passing methods without overwriting, you must ensure that the variable names are different.
3. Use TempData to transmit values
The following code is available in Action: TempData ["name"] = "Vibin3 ";
You can receive the following message in the View: name: @ TempData ["name"]
Note:TempData is stored in the Session. This method can be used across actions. The Controller obtains TempData from the Session and clears the Session during each request. Therefore, data in TempData can only be transmitted once by the Controller.
4. Take the object as a parameter when returning the view and receive it through the Model.
The following code is available in Action:
Person p = new Person () {Name = "Vibin4 "};
Return View (pig );
The Person class is defined as follows:
Public class Person
{
Public string Name {get; set ;}
}
You can receive the following message in the View:
Name: @ {Person p = Model as Person;} @ p. Name
Note:: On the view page, you need to introduce the namespace of the Person class, and the Person must be defined as public. In essence, this is also carried out through the Model object in ViewData. Therefore, for the View () overload, as long as the parameter with the object model can be passed to the value, that is, the reload marked by the red box.
5. You can use the Redirect () method to pass in the Url. You can consider adding parameters after the Url.
In Action, there is the following code: return Redirect ("/Another/Index? Name = Vibin5 "); // Another is Another controller name
You can receive the following message in the View: name: @ Request. Params ["name"]
Note:The view is the view corresponding to the Action under another Controller.
If the Url is in this way: "/Another/Index/Vibin5 ",
In the view, you can receive: Name: @ Html. ViewContext. RouteData. Values ["id"]
Similarly, use @ Request. requestContext. routeData. values ["id"] Or @ Html. viewContext. routeData. route. getRouteData (Html. viewContext. httpContext ). values ["id"] can also receive Values. This is the method under the default routing rule. The default route is set to {controller}/{action}/{id}. The routing rule is not changed, the variables received in the view can only be written with id and cannot be changed to name.
6. You can use the RedirectToAction () method. This method returns the redirected result object. In this method, the object is passed as a parameter.
Return RedirectToAction ("Index", "Another", new {Name = "Vibin6"}); // borrow the Person class defined earlier
You can receive the following message in the View: name: @ Request. Params ["name"]
Note:The view is the view corresponding to the Action under another Controller. The RedirectToAction () method has the following heavy loads. Any parameter with the object routeValues can be used to pass the object. For details, refer to the red box ID.
*In addition, in the examples of 5th and 6th, the Request. Params [] used for receiving Url parameters can also be changed to Request [] or Request. QueryString [].
How does Struts2 transmit data between the view layer (jsp) and the business layer (action?
Jsp is uploaded to the action, which is submitted through url and <form> </form>.
Such as login. do? Num = bill & password = 123. In the action, you can get the account and password through request. getParameter. If you submit in form, you must set getter and setter in action to obtain the submitted account and password.
The action is uploaded to jsp, which can be url-based. You can also transmit data through request. setAttribute () or session. setAttribute.
Value passed by action
In fact, it is very convenient for you to verify it. In sevlet, you can directly use request. getParameter to get this value and see if it can be passed in.
Actually? Name = value this is a basic method for passing values. Generally, form actions are not used in this way, because the writing is to solve the problem that forms cannot be submitted more often, of course you can write it like this.