ASP. net mvc key tutorial version of the first anniversary of the third back controller and view

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.