Learning ASP. net has been around for a month and has been half-baked, Because Java development has been around for a long time before, and there is no time to calm down and learn, plus ASP. NET framework is also very complex, and it is also a turn-off at a time, so you can only see a little record, slowly accumulate, and slowly summarize it.
It is very easy to transfer data from an action to a view, and there are many methods. The most direct thing is to pass a model to the view. This is the meaning of MVC. If some messages such as error messages are displayed, you can use viewdata:
PublicActionresult index () {viewdata ["Message"] ="Hello word!";ReturnView ();}
Then the View:
< ASP: Content ID = "Indexcontent" Contentplaceholderid = "Maincontent" Runat = "Server" > <% : Viewdata [ " Message " ] %> </ ASP: Content >
The actual results are as follows:
We can see that viewdata is a dictionary that stores key-value pairs.
Since action can pass data to view, can view modify data and then send it back to action?
WeCodeModify it as follows:
Public Actionresult index () {viewdata [ " Message " ] =" Hello word! " ; Return View ();} Public Actionresult about (){ String Message = viewdata [ " Message " ] As String ; If (Message =" Hello " ) {Viewdata [ " Message2 " ] = " Modified " ;} Else {Viewdata [ " Message2 " ] =" Not modified " ;} Return View ();}
View:
Why? Because viewdata is only valid in one HTTP request, the value is automatically cleared after the request is completed. Why?
Because the lifecycle of viewdata is the same as that of the requested view, it is only valid for the current view.
Viewbag can also be used to transmit data to a view:
PublicActionresult index () {viewbag. Message="Hello, word";ReturnView ();}
View:
<ASP: ContentID= "Indexcontent"Contentplaceholderid= "Maincontent"Runat= "Server"><%: Viewbag. Message%></ASP: Content>
Viewbag is equivalent to viewdata.
Like viewdata, it is a dictionary value, but its internal implementation is completely different.
The biggest advantage of viewbag is that it can use its values without transformation, but viewdata needs to do this:
StringMessage = viewdata ["Message"]As String;
Because viewbag stores not key-value pairs, but dynamic types, which are newly added to ASP. NET mvc3. Dynamic types are very powerful, especially for containers.
Viewbag is the encapsulated viewdata, which is generated in accordance with the dynamic keyword of C #4. It allows us to retrieve the values in the dictionary like attribute access, which is more natural. This is also advocated by C #: "readable code ".
Viewbag is not inferior to viewdata in terms of usage, but it is certain that viewbag is slower than viewdata, but this can be ignored. It is worth noting that viewbag can directly access the data stored in viewdata (because it is only encapsulated viewdata ). However, for example:
Viewdata ["Message hehe"] ="Hello, word";
Viewbag is powerless.
More importantly, viewbag cannot be used as a parameter of the extension method, because the compiler must know the true type of the parameter to ensure that the selected extension method is correct, viewbag cannot be used for HTML auxiliary methods.
In addition to the above two (in fact, one), there is also a method, that is, tempdata.
Tempdata is also a dictionary, so it is equivalent to viewdata, but there is a big difference between the two.
Tempdata, as its name implies, is temporary data. Tempdata is stored in the session. The controller obtains tempdata from the session every time it sends a request, and then clears the session. Based on this fact, the lifecycle of tempdata ends after each request. There is a saying on the Internet: tempdata can be transferred only once through the Controller. Assume that our controller is redirected to the next controller, and so on. In the corresponding view of the last controlle, The tempdata can be obtained. However, this does not mean that tempdata has been transferred across requests. This is still only one request. As to whether it is transferred across controllers, I don't think so, in the preceding example, only the final controller can pass the tempdata to the view. Therefore, tempdata can only be passed through the Controller once.
Tempdata can also be passed between actions, such:
Public Actionresult index () {tempdata [ " Message " ] = " Hello " ; Return View ();} Public Actionresult about (){ If ( " Hello " = Tempdata [ " Message " ] As String ) {Tempdata [ " Message " ] = " Hello, word " ;} Return View ();}
However, you must note that to make the behavior correct, that is, tempdata can be passed to another action, the view corresponding to the index operation cannot use tempdata, because once used, it is equivalent to a request, the value in tempdata is cleared.
Tempdata is stored in the session, but this is not absolute. We can change the storage location of tempdata, as long as the itempdataprovider interface is implemented, but it is generally not necessary to do so.