MVC5.0 Knowledge Point Carding

Source: Internet
Author: User
Tags http post

When we use MVC, we may always use the knowledge we have been familiar with to achieve existing functions, and to comb some knowledge points so that each function can be implemented in a variety of ways.

We always use the scaffolding features of Microsoft MVC when developing small systems, such as routing may be using the default routing, in a slightly complex or large system we can actually customize the route.

Routing Constraints

routes. MapRoute (

Name: "Language",

URL: "{language}/{controller}/{action}/{id}",

defauts:new {controller= "Home", action= "Index", id=urlparameter.optional},

contrains:new {[Email protected] ' (en) | ( DE) "}

Upper constraint by use (en) | (DE), the language parameter is defined only by en or de. Examples: Http://<server>/en/Home/About or http://<server>/de/home/About are legal.

Action Method

(1) Action methods can be declared with 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 a routing configuration

The default route has an ID parameter, so the above method can be declared as follows:    

public string greeting (string id)

{

Return Httputility.htmlencode ("Hello," +id);

}

Called as follows: HTTP://<SERVERNAME>/HOME/GREETING/1

(3) Action method can be declared as multiple parameters

public int Add (int x,int y)

{

return x+y;

}

This action can be called using the following URL to populate 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 follows:

routes. MapRoute (

Name: "Multipleparameters",

URL: "{controller}/{action}/{x}/{y}",

defauts:new {controller= "Home", action= "Index"}, can be invoked using HTTP://<SERVER>/HOME/ADD/7/2;

View

Passing data to a view-the controller and view are running in the same process. Views are created directly within the controller, which facilitates the transfer of data from the controller to the view;

The razor syntax (/using @ as the migration character) can automatically detect the end of C # code, but in some cases this cannot be automatically seen. At this point, you can use parentheses to tag variables, for example:

<div>@ (name), Stephanie</div> sometimes it is not recognized that you can try another way: @: To show where the definition text begins

Layout

All view pages use layout pages by default, and if the current page does not use layout pages, you need to set the layout property to Null to explicitly specify:

@{

Layout=null;

}

The _viewstart.cshtml page contains the default configuration for all views. The only setting that is defined by default is to set the layout property to Shared Layout page _layout.cshtml.

using Partitions

You can use @section to specify the block code, and then the layout page can refer to different modules in the loaded view.

submitting data from the client       

@{  @:viewbag.title = "Create Menu";} 

  By default, the HTTP request method is get, and after you apply the HttpPost attribute, the request method is post. To read httppost data, you can use the information in the Request object.

But it is much simpler to define CreateMenu methods with parameters. The name of the parameter 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 with the same name)

{

......

}

Model Binder

In addition to using multiple parameters in an action method, you can also use a type that 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 is responsible for transmitting the data in the HTTP POST request. The model binder implements the Imodelbinder interface. By default. To bind an input field to a model using the Defaultmodelbinder class

Annotations and validation

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;}
}

The attributes used for validation include the Compareattribute used to compare different attributes, the Creditcardattribute used to validate the credit card number, the Emailaddressattribute used to verify the e-mail address, to compare the input to the enumeration value

Enumdatatypeattribute, and the phoneattribute used to verify the phone number, in order to use the validation attribute, you can use Modelstate.isvalid to verify the state within the action method.

When we define a model that cannot be modified, we can define another model that has the same properties and add the model using attributes to the classes that need to be constrained.

For example: Define the Menumetadata class:

public class Menumetadata

{

public int Id {get; set;}
[Required, Stringlength (+), 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;}

}

Large systems are made up of fragmented knowledge points, and good foundations are good foundations.

MVC5.0 Knowledge Point Carding

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.