This article is a mirror article of machine translation, please click here to jump to the original article.

View: 34235|Reply: 1

[ASP.NET] Manually validate model fields ASP.NET MVC

[Copy link]
Posted on 2020-12-12 20:12:13 | | | |
Regarding model validation, I have written about it before:

webapi ModelState.IsValid model validation
https://www.itsvse.com/thread-3809-1-1.html

Asp.net Core Model Data Validation
https://www.itsvse.com/thread-9016-1-1.html

We usually call the following code in the background to verify that the model meets the validation requirements:

If we are not a web program, how can we manually validate model fields? For example: console programs, winform, etc.

This article still uses Web MVC to validate the model, but it is a manual call to validate.

First, we define a custom validation feature that the validation string must contain the "itsvse" content, as follows:



All model validation features need to be inherited from:System.ComponentModel.DataAnnotations.ValidationAttribute

The object model is as follows:

The form code is as follows:



The form content is submitted to the /Home/Test controller method, and we manually verify the parameters of the post, as follows:

Try clicking on the "Submit button" as shown below:


False
Name : Field: Name, maximum length: 10, minimum length: 3

Content: The string must contain: itsvse
Try to submit the correct content, as shown below:



And alsoThe second wayYou can also implement manual validation through code like the following, reflecting the IsValid method to get all the features of the fields, and then executing the IsValid method for the features, howeverNot recommended, the code is as follows:

(End)




Previous:.NET/C# to get disk usage
Next:.NET Core、. NET Standard, ASP.NET Core, and .NET Framework relationships
 Landlord| Posted on 2021-4-13 17:52:14 |
Validate validation does not support List collections
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com