|
|
Posted on 2020-12-12 20:12:13
|
|
|
|

Regarding model validation, I have written about it before:
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 usageNext:.NET Core、. NET Standard, ASP.NET Core, and .NET Framework relationships
|