Introduction to MVC

Source: Internet
Author: User

Summarize the recently learned MVC content according to your own understanding. I hope you can give some advice on the mistakes.

 

Principle of MVC

The process is: URL (the address entered by the user)-> routes (route address processing to find the correct address)-> controllers (controller, the internal methods correspond to pages with the same name. These methods are collectively referred to as action)-> models (getting database data)-> View (return page)

 

M (model layer) is mainly responsible for business logic and database interaction.
View (view layer) is used to display and submit data.
C is the controller. It is mainly used to capture requests and control request forwarding.

R indicates that the page address is parsed by routes.

 

Form submission 

Method 1. <% using (html. beginform ("html", "home") // It is home instead of homecontroller

{%>

Enter the User name: <% = html. Textbox ("name") %>

Enter Password: <% = html. Password ("name") %>

<Input id = "submit1" type = "Submit" value = "Submit"/>

<% }%>

Method 2.

<Form ID = "form1" method = "Post" Action ="

<% = Html. attributeencode (URL. Action ("html", "Beta") %> // beta is the controller, HTML is the action

 

</Form>

Pass Value

1. Pass the value from the Controller to the page:

Controllers (in the corresponding action ):

Viewdata ["test"] = "Holle ";

 

View (where output is needed ):

<Div> <% = html. encode (viewdata ["test"]) %> </div>

 

2. Pass the value from the form on the page to the Controller:

Method 1. Automatic ing

To use this method, we only need to define the parameter name of the corresponding action method as the control name, Asp. net MVC Framework will automatically help us map and directly assign the form data to the method parameter corresponding to the form action,

 

View Source

Print?

01 public ActionResult Test(string Textbox1,string Textbox2)// Textbox1, textbox2 is the name of the control in the form
02         {
03             if (Request.HttpMethod == "POST")
04             {
05                 ViewData["test"] = a;
06             }
07             else
08             {
09                 ViewData["test"] = aa;
10             }
11             return View();
12         }

Method 2. Obtain the form function
String = request. Form ["textbox1"];

 

Htmlhelper Extension

Customize a class

View Source

Print?

01 using System;
02 using System.IO;
03 using System.Runtime.CompilerServices;
04 using System.Web;
05 using System.Web.Mvc;
06 using System.Web.Routing;
07   
08 namespace System.Web.Mvc.Html
09 {
10     public static class Test
11     {
12         public static void test(this HtmlHelper html,string names,string name,string value)
13         {
View Source

Print?

1 return string.Format("<label for='{0}'>{1}</label><br />", name, value);
View Source

Print?

1         }
2     }
3 }
4 For this class, I am surprised that it is not an inherited interface, but a method parameter is added.this Htmlhelper HTML is even a problem. I have never seen it before.

Return string. format ("<label for = '{0}'> {1} </label> <br/>", name, value ); this is to directly create the desired label Label and parameters.

The principle is to dynamically load the above method from a class to the htmlhelper class, and then use HTML on the view page. test ("", "") to dynamically generate HTML code and fill in parameters.

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.