c# - Detect whether or not a specific attribute was valid on the model -


I created my own recognition feature from the system. Companent MODEL. Data Annotation Verification, I want to be able to find out from my controller, whether it was valid on specific feature model or not.

My setup:

  Public class MyModel {[Required] [CustomValidation] [Some other validity] Public string SomeProperty {Received; Set; }} Public class CustomValidationAttribute: validation attribute {public override bool} IsValid (object value) {/ custom verification logic here}}  

Now, how can I find out from the controller Did the validity of CustomValidationAttribute be successful?

I'm looking at ModelError's exception property in ModelState, but there is no way to add a custom exception to my CustomValidationAttribute.

I have now tried to test a specific error message in ModelState:

  Public ActionResult Postmodel (Maymold Models) {if (ModelState.Where (i = & Gt; i.Value.Errors.Where ((E = & gt; E.ream Message == Custom Validation Attribute. Chever Message)) Any ()). Any ()) DoSomeCustomStuff (); // the remaining verb here}  

and my custom validation inversion has been changed to:

  public class CustomValidationAttribute: validation admit {public static string SharedMessage = "CustomValidationAttribute Error"; Public Override Bull ISAid (Object Value) {Error Message = Shared Message; // custom verification logic here}}  

I do not like to trust string mailing, and thus error message has been misused.

What is my choice?

I think this is important for CustomEvalidationAttribute Enum named ExceptionType Identifies the type of clearly raised exception

In the controller, we can check the exception type and handle it accordingly.

  try {} hold (exception e) {switch (e.exceptionType) {case ExceptionType.Val1: // Brake handle accordingly; }}  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -