Document directory
- 1. Create a controller
- 2. Create a view
- 3. Edit the controller and view to complete a simple page value transfer
This topic describes how to use ASP. net mvc to compile the desired page. The specific process is as follows (currently, csdn prohibits image uploading, so you can only copy this part from the Internet. All code has passed the test, so please feel free to use it ).
1. Create a controller
Right-click the controllers folder under solution-> select Add-> select the Controller option, as shown in:
A dialog box is displayed, as shown in:
We can name it at will. Here we name it rocketcontroller.
The Controller folder contains rocketcontroller. CS files. The default generated code is:
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/> return view (); <br/>}</P> <p >}< br/>
2. Create a view
Of course, in addition to the Controller, we also need to create a view. First, we need to create a rocket folder in views, and then we need to create an index. aspx in it.
However, ASP. net mvc also provides a shortcut for creating a view. Right-click the action of the corresponding controller and choose add view. The latter is used in this article. As shown in:
A dialog box is displayed, as shown in:
After determining the View File Name and master file, click Add to create a View File. The Default Code of index. aspx is:
<% @ 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> <H2> index </H2> </P> <p> </ASP: content>
3. Edit the controller and view to complete a simple page value transfer
Modify the code of rocketcontroller. CS:
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 (string ID) <br/>{< br/> viewdata ["chsword"] = ID; <br/> return view (); <br/>}</P> <p >}< br/>
The views/Rocket/index. aspx code is changed:
<% @ 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> <% = viewdata ["chsword"] %> </P> <p> </ASP: content> <br/>
Next we will access/Rocket/index/helloweice, and we can see the following page:
In this way, a value is uploaded from the URL to the Controller, and then from the Controller to the view. From the above section, we can see that the string ID of the Action parameter is used to receive the ID section of {controller}/{action}/{ID. Viewdata is an idictionary between pages used by the Controller to transmit data to the view. In this way, view and controller can collaborate to complete page display and logic processing.