ASP. MVC3 data Validation (iv) a consideration for-remote validation

Source: Internet
Author: User

Original: ASP. MVC3 data Validation (iv) a consideration for-remote validation

        The first few methods of validating the model-based data in ASP. MVC3 are all done, and this section is purely about a problem I've encountered, because it's related to data validation, so it's in this series of articles.          after the sharing of the previous articles, we should think that the remote data from ASP. NET is more useful, but I encountered problems in the actual use.          first, in general, validation of a property may require more than one remote authentication, for example, for a user name, we need to make some restrictions on its length, This can be solved by the stringlength feature, and it is necessary to verify that the user name cannot be duplicated, which needs to be solved by the remote feature.          now we need to add another validation, that is, the user name cannot contain forbidden words. Like what...... (more sensitive, do not write ...) ^_^), the forbidden Word is stored in the database. At first I think this is also simple, add a remote verification does not solve the ... Children's shoes, sometimes we feel very simple to do things, if not to do, never know if you can do. So we recommend that you do not procrastinate and do it as soon as possible.          crackling, soon the verification method is written, and then added to the model of a look dumbfounded ...          originally does not support the same property has two remote authentication ...           What to do at this time, try to write two validations in the same remote, but this will only show a errormessage, Be rejected. (But I thought that the method of the remote attribute could only return a bool value and not return errormessage).          then I was in Cnblogs, CSDN and Baidu to know the post inquiry, most of the answers did not mention the focus.           because the time is tight (and not because of the time is tight, can not let such a small verification with the manager to talk about a day), so I wrote a custom validation solution.          Although the matter is solved, but always feel that Microsoft design is always not so silly fork, even this did not think. But the data also did not find what, and then kept.           until yesterday, a buddy on Csdn said remote verification can be customized errormessage, I realized, I used to think that I could only return true or false.          then it was easy, and it took 5 minutes to find a way to return the errormessage, which would have been verified as false without returning a bool value. You can return directly to errormessage. Here's how:

         PublicJsonresult Checkusername (stringuserName) {            if(Isuniquename (UserName) &&Isforbiddenname (UserName)) {                returnJson (true, Jsonrequestbehavior.allowget); }            Else if(!Isuniquename (UserName)) {                returnJson ("user name is not unique! ", Jsonrequestbehavior.allowget); }            Else            {                returnJson ("user name does not contain forbidden words! ", Jsonrequestbehavior.allowget); }        }

         this solves the problem of requiring multiple remote authentication.          Although the problem is solved, but still have to spit down the slot Microsoft, why not multiple remote verification, bar so many logic in a method is easy to go around the halo amount ...           Although this is a minor problem, it has at least two points of inspiration from it:             1, see the problem is not terrible, terrible is a problem but we did not see;             2, many things, as long as you insist, will always wait until steady day.                      previous articles in ASP. MVC3 data Validation (i) ASP. NET MVC3 data Validation (ii)--Customization and localization of error messages ASP. MVC3 data Validation (iii)-Custom data annotations

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.