Asp.net mvc3 data verification (IV)-Precautions for remote verification

Source: Internet
Author: User
The first few articles have completed the model-based primary data verification methods in Asp.net mvc3. This section is just about a problem I encountered, because it is related to data verification, so I put it in this series of articles. After sharing the previous articles, we should think that the remote data annotation provided by Asp.net is relatively easy to use, but I encountered problems in actual use. First, generally, more than one remote verification is required for an attribute. For example, for a user name, we need to limit its length. This can be solved through the stringlength feature; at the same time, you also need to verify that the user name cannot be repeated, which needs to be resolved through the remote feature. Now you need to add another verification, that is, the user name cannot contain prohibited words. For example,... (if it is sensitive, it will not be written... ^_^), The prohibited words are stored in the database. At the beginning, I thought this was simple, but I couldn't solve it by adding a remote verification... Sometimes, we think it is very easy to do things. If we don't do it, we never know if you can do it. Therefore, we recommend that you do not drag on things and do it as soon as possible. Then I wrote the verification method and added it to the model... In the past, two remote verification methods were not supported for the same attribute... What should we do at this time? Try to write the two verifications in the same remote, but in this case, only one errormessage can be displayed and the verification is rejected. (However, I thought that the remote method can only return one bool value, but cannot return errormessage ). Then I posted a question on cnblogs, csdn, and Baidu. Most of the answers did not focus on the question. Because of the tight schedule (not because of the tight schedule, it is impossible to let such a small verification talk to the manager for a whole day), so I wrote a custom verification solution. Although the problem was solved, I always thought that Microsoft would not be so stupid during design, and I didn't even think of it. However, I did not find any information, so I kept it. It was not until yesterday that a buddy on csdn said that remote verification could customize errormessage. I suddenly realized that I had always thought that only true or false would be returned. The next step is very simple. It took 5 minutes to find the method for returning errormessage. If it was originally verified as false, no bool value is returned, and errormessage can be directly returned. The method is as follows:
Public jsonresult checkusername (string username) {If (isuniquename (username) & isforbiddenname (username) {return JSON (true, jsonrequestbehavior. allowget);} else if (! Isuniquename (username) {return JSON ("the user name is not unique! ", Jsonrequestbehavior. allowget);} else {return JSON (" the user name does not contain any banned word! ", Jsonrequestbehavior. allowget );}}

 

In this way, the problem requiring multiple remote verification can be solved perfectly. Although the problem has been solved, I still need to talk about Microsoft. Why can't I perform multiple remote verification? So many logics are easily confused in one method... Although this is a small problem, we get at least two inspirations: 1. The problem we see is not terrible. What is terrible is that we did not see it; 2. Many things, as long as you stick to it, it will always wait until one day. Previous articles on ASP.net mvc3 data verification: Asp.net mvc3 data verification (I) Asp.net mvc3 data verification (II) -- custom error messages and local Asp.net mvc3 data verification (III) -Custom Data Annotation

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.