In this section, let ASP. net mvc really run.
1. Create a controller
First, we create a new controller by ourselves. Right-click the Controller on the controllers and add the Controller option.
Then a dialog box is displayed:
Here we name it eicecontroller.
Note: This is a pure advertisement. If you are not interested, skip this line: www.eice.com.cn to create a web Social Network website for you.
The default generated code is as follows:
// Do not remember what I mentioned earlier. All controllers must inherit from the Controller class.Public ClassEicecontroller: controller {PublicActionresult index (){ReturnView ();}}
Ii. Create a View File
Of course, in addition to the Controller, we also need to create a view. Create an eice folder in views first, and then 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.
Then a window is displayed.
After determining the View File Name and master file, click Add to create a View File.
<% @ Page title = "" Language = "C #" masterpagefile = "~ /Views/shared/site. Master "inherits =" system. Web. MVC. viewpage "%> < ASP : Content ID = "Content1" Contentplaceholderid = "Titlecontent" Runat = "Server" > Index </ ASP : Content > < ASP : Content ID = "Content2" Contentplaceholderid = "Maincontent" Runat = "Server" > < H2 > Index </ H2 > </ ASP : Content >
If you want to create a page without a master, check the select master page.
3. Edit the controller and view to complete a simple page value transfer
We changed the index of eicecontroller
Public ClassEicecontroller: controller {PublicActionresult index (StringID) {viewdata ["Chsword"] = ID;ReturnView ();}}
Rewrite it in the View File/views/eice/index. aspx.
<% @ Page title = "" Language = "C #" masterpagefile = "~ /Views/shared/site. Master "inherits =" system. Web. MVC. viewpage "%> < ASP : Content ID = "Content1" Contentplaceholderid = "Titlecontent" Runat = "Server" > Index </ ASP : Content > < ASP : Content ID = "Content2" Contentplaceholderid ="Maincontent" Runat = "Server" > <% = Viewdata ["chsword"] %> </ ASP : Content >
Next we will access/eice/index/helloweice. We can see:
In this way, a value is uploaded from the URL to the Controller, and then from the Controller to the view.
From aboveProgramThe string ID of the Action parameter is used to receive the ID 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.
In addition to viewdata, what other methods can we use the Controller to transmit values to the View File? Can we display other files besides aspx files?
Reference: PV version Asp.net MVC Framework III (Controller and view)