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 |
03 |
if (Request.HttpMethod == "POST" ) |
09 |
ViewData[ "test" ] = aa; |
Method 2. Obtain the form function
String = request. Form ["textbox1"];
Htmlhelper Extension
Customize a class
View Source
Print?
03 |
using System.Runtime.CompilerServices; |
06 |
using System.Web.Routing; |
08 |
namespace System.Web.Mvc.Html |
10 |
public static class Test |
12 |
public static void test( this HtmlHelper html, string names, string name, string value) |
View Source
Print?
1 |
return string .Format( "<label for='{0}'>{1}</label><br />" , name, value); |
View Source
Print?
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.