Get the range and Stringlength validation attribute settings for model properties in MVC

Source: Internet
Author: User

The client and server-side model validation information in MVC is hosted by the Modelmetadata type, and after the modelmetadata of the property is obtained (it is not yet known how to obtain Modelmetadata's child shoes, please do it yourself), We can easily get some of the features we define in the model, such as the display name, whether it is read-only, whether it must be, comments, data types, and so on, but does not include both the range and Stringlength attributes that need to be validated. I turned the Modelmetadata object upside down and didn't find the value set.

Thus, the definition information of an attribute can only be obtained by reflection, and the following code can obtain all the attributes defined by the attribute:

var attrs = metadata. Containertype.getproperty (metadata. PropertyName). CustomAttributes;

Then we traverse it, looking for the type we want, here's an example of range:

foreach (var in attrs)            {                iftypeof(rangeattribute) && f.constructorarguments[0typeof  (Type))                {                    //TODO: Value                 }            }

The code first determines whether the type is consistent, and then it determines whether the first construction parameter is not of type (the range attribute has three constructors, where the first parameter is type Max and Min values can be any type, where we do not need to process the data of this constructor, Only two other types of transport int or double are interested in the constructor.

Then you can take a value:

Double Min = ((iconvertible) f.constructorarguments[0]. Value). ToDouble (null); Double Max = ((iconvertible) f.constructorarguments[1]. Value). ToDouble (null);

Here we need a value of type double, so the conversion is converted to double, regardless of whether an int or a double type parameter is passed in.

This allows us to make basic restrictions on sending to the client page:

<type= "number"  name= "points"  min= "@Min" Max = "@Max" />

Similarly, you can take stringlength information to limit the input character length of the text input box:

<maxlength= "@Length">

You can also combine JS to write some custom client authentication.

The server-side model validation does not bother us, these two features are triggered correctly during validation.

Using this technique, you can also implement some custom features to control the client page functionality.

Get the range and Stringlength validation attribute settings for model properties in MVC

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.