MVC5.0 knowledge point sorting, mvc5.0 knowledge point
When using MVC, we may always use the knowledge points we have been familiar with to implement the existing functions, and sort out some knowledge points to diversify the implementation methods of each function.
We always use the scaffolding function of Microsoft MVC when developing small systems. For example, the routing may use the default routing. In a slightly complicated or large system, we can actually customize the routing.
Route Constraints
Routes. MapRoute (
Name: "Language ",
Url: "{language}/{controller}/{action}/{id }",
Defauts: new {controller = "Home", action = "Index", id = UrlParameter. Optional },
Contrains: new {language = @ "(en) | (de )"}
By using (en) | (de), the language parameter can only be en or de. for example, http: // <server>/en/Home/About or http: // <server>/de/home/About are valid.
Action Method
(1) Action methods can be declared as parameters
Public string Greeting (string name)
{
Return HttpUtility. HtmlEncode ("Hello," + name );
}
With this statement, you can use http: // <server>/Home/Greeting?Name = Danile(URL string) to call this method.
(2) You can declare the route configuration.
The default route has the Id parameter. Therefore, the preceding method can be declared as follows:
Public string Greeting (string id)
{
Return HttpUtility. HtmlEncode ("Hello," + id );
}
The call method is as follows: http: // <ServerName>/Home/Greeting/1
(3) Action methods can be declared as multiple parameters
Public int Add (int x, int y)
{
Return x + y;
}
You can use the following url to call this action to fill in the values of the x and y parameters: http: // <server>/Home/Add? X = 4 & y = 5
Of course, you can also define a route to pass values in different links, as shown below:
Routes. MapRoute (
Name: "MultipleParameters ",
Url: "{controller}/{action}/{x}/{y }",
Defauts: new {controller = "Home", action = "Index"}, which can be called using http: // <server>/Home/Add/7/2;
View
Pass data to the view-the controller and view run in the same process. The view is directly created in the controller, which facilitates data transmission from the Controller to the view;
The Razor syntax (/use @ as the Migration character) can automatically detect the end of the c # code, but in some cases, this is not automatically visible. You can use parentheses to mark variables. For example:
<Div> @ (name), Stephen </div>, sometimes you can try another method: @: to display the start position of the definition text.
Layout
All view pages use Layout pages by default. If the current page does not use Layout pages, you need to set the Layout attribute to null to explicitly specify:
@{
Layout = null;
}
The _ ViewStart. cshtml page contains the default configuration for all views. The only setting defined by default is to set the Layout attribute to the shared Layout page _ Layout. cshtml.
Use Partition
You can use @ section to specify the block code, and then the layout page can reference different modules in the loaded view.
Submit data from the client
@{ @:ViewBag.Title = "Create Menu";}
By default, the http request method is GET. After the HttpPost feature is applied, the request method is POST. To read httpPost data, you can use the information in the Request object.
However, it is much easier to define the CreateMenu method with parameters. The parameter name matches the name of the form field.
[HttpPost]
Public ActionResult CreateMenu (int id, string text, double price, string category) // the same name as the form field in form (name)
{
......
}
Model Binder
In addition to using multiple parameters in the Action method, you can also use the type. The type attribute matches the input field name:
[HttpPost]
Public ActionResult CreateMenu (Menu m)
{
ViewBag. Info = string. Format (
"Menu created: {0}, Price: {1}, category: {2}", m. Text, m. Price,
M. Category );
Return View ("Index ");
}
The model binder transmits data in the http post request. The model binder implements the IModelBinder interface. By default. Bind the input field to the model using the defamodelmodelbinder class
Annotation and Verification
Public class Menu
{
Public int Id {get; set ;}
[Required, StringLength (25)]
Public string Text {get; set ;}
[DisplayName ("Price"), DisplayFormat (DataFormatString = "{0: C}")]
Public double Price {get; set ;}
[DataType (DataType. Date)]
Public DateTime Date {get; set ;}
[StringLength (10)]
Public string Category {get; set ;}
}
Verification features include: CompareAttribute used to compare different attributes, CreditCardAttribute used to verify the credit card number, EmailAddressAttribute used to verify the email address, used to compare the input and enumeration values
EnumDataTypeAttribute and PhoneAttribute used to verify the phone number. To use the verification feature, you can use ModelState. IsValid in the action method to verify the status.
When we define a model that cannot be modified, we can define another model with the same attributes and add the features of this model to the class to be constrained.
For example, define the MenuMetadata class:
Public class MenuMetadata
{
Public int Id {get; set ;}
[Required, StringLength (25), CreditCard]
Public string Text {get; set ;}
[DisplayName ("Price"), DisplayFormat (DataFormatString = "{0: C}")]
Public double Price {get; set ;}
[DataType (DataType. Date)]
Public DateTime Date {get; set ;}
[StringLength (10)]
Public string Category {get; set ;}
}
[MetadataType (typeof (MenuMetadata)]
Public partial class Menu
{
Public int Id {get; set ;}
Public string Text {get; set ;}
Public double Price {get; set ;}
Public DateTime Date {get; set ;}
Public string Category {get; set ;}
}
A larger system is composed of fragmented knowledge points. A good foundation is a good foundation.