Document directory
- I. Passing objects to the view page through viewdata and tempdata attributes
- 2. Passing objects through viewdata. Model
The previous article details how to use ASP. net MVC creates its own page. This article introduces how to use viewdata to pass values from the Controller to the view file. This new problem occurs again, can we display other files besides aspx files?
This article describes in detail how to pass values to a view.
I. Passing objects to the view page through viewdata and tempdata attributes
As mentioned above, viewdata can be used to transmit data from the Controller to the view. In the previous article, we established the rocketcontroller class. In this example, we will change this controller.
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. MVC; <br/> using system. web. MVC. ajax; </P> <p> namespace movieapp. controllers <br/>{< br/> public class rocketcontroller: controller <br/>{< br/> // <br/> // get: /Rocket/</P> <p> Public actionresult index () <br/> {<br/> viewdata ["viewdata"] = "display viewdata" here "; <br/> tempdata ["tempdata"] = "show tempdata"; <br/> return view (); <br/>}</P> <p> Public actionresult index2 () <br/>{< br/> return view ("Index "); <br/> // The specified view file is the index in the eice directory. aspx </P> <p >}< br/>
We remove the index parameter and provide values for viewdata and tempdata. In View/Rocket/index. aspx, write the following code:<% @ Page title = "" Language = "C #" masterpagefile = "~ /Views/shared/site. master "inherits =" system. web. MVC. viewpage "%> </P> <p> <asp: content ID = "content1" contentplaceholderid = "titlecontent" runat = "server"> <br/> index <br/> </ASP: content> </P> <p> <asp: Content ID = "content2" contentplaceholderid = "maincontent" runat = "server"> </P> <p> 1. <% = viewdata ["viewdata"] %> <br/> 2. <% = tempdata ["tempdata"] %> </P> <p> </ASP: content> <br/>
Note that the above 1.2 is not a row number and is written by me.
Run the project and access http: // localhost: 1750/Rocket/index:
1. viewdata is displayed here
2. Show tempdata here
Access http: // localhost: 1750/Rocket/index2 and the result is as follows:
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 only valid in the current action. However, tempdata can still exist on other pages like session, but only one page of access (similar to flash in monorail) tempdata is generally used for temporary cache content or passing error information when an error page is thrown.
2. Passing objects through viewdata. Model
Create a model: rocketindexmodel. CS. The main code is as follows:
Public class rocketindexmodel <br/> {<br/> // <summary> <br/> // name <br/> /// </Summary> <br/> Public string name {Get; set ;} <br/> // <summary> <br/> // gender <br/> /// </Summary> <br/> Public bool sex {Get; set ;}</P> <p>}
Then we create a new action: index3:
Public actionresult index3 () <br/>{< br/> var M = new rocketindexmodel <br/>{< br/> name = "Xue jingming ", <br/> sex = true <br/>}; <br/> return view (m); <br/>}< br/>}
Next we will create a View File for index3, and we will still right-click addview on index3.
Therefore, a view file is automatically generated, and the result is as follows:
What should we do if we need to display other files?