asp.net mvc - Validating Enum Values within C# MVC. Partial validation occurs - How to change validation behaviour? -
I am representing an enum in my razor view as a hidden area, that is, on an action result Is posted back. / P>
I've found that when it adds the string value provided within HTML, it automatically validates the value of the enum.
/// & lt; Summary & gt; /// Quiz Type Enum /// & lt; / Summary & gt; Public Anonymous Quiz Type {/// & lt; Summary & gt; /// Scored Quiz /// & lt; / Summary & gt; Score = 0, /// & lt; Summary & gt; /// Personality Type Quiz /// & lt; / Summary & gt; Personality = 1}
RAZR:
@ html.HiddenFor (x => x.QuizType)
< P> Rated HTML: & lt; Input data-val = "true" data-val-required = "QuizType is not valid" id = "QuizType" name = "QuizType" type = "hidden" value = "scanned" & gt;
If I change the value within the DOM to do something wrong and submit the form, then ModelState.IsValid
returns false
and the following error is added to the modelstate:
"value 'myincorrectvalue' is not valid for quiz type."
It's all gravy, but I thought that if I made a visual model, then I have to explicitly set up the verification rule on my visual model, like [required]
attribute.
In addition to this column EnumDataType
is a verification feature specifically for [EnumDataType (typeof (QuizType)] Public QuizType QuizType {get; Set; }
Question
If verification occurs automatically when binding, EnumDataType
The thing is attribute?
OK, so I got the answer to my own question.
The error was not possible when the error message was more than a normal error message. When compulsive attempts to force no existence string of enum values posted from HTML to enum, this error generates this error:
value 'myincorrectvalue' quiz type
The exact same error message will appear if I try to bind a string value from int
into my visual model class.
It seems that the problem is that along with representing the string, a NAM can be any integer value. I can set enum any number, even if that number is not present in my enum.
/// & lt; Summary & gt; /// Quiz Type Enum /// & lt; / Summary & gt; Public Anonymous Quiz Type {/// & lt; Summary & gt; /// Scored Quiz /// & lt; / Summary & gt; Score = 0, /// & lt; Summary & gt; /// Personality Type Quiz /// & lt; / Summary & gt; Therefore, it is valid and my ANUUM value will be bound without error, even if 1000
is not present within my value:
/ P>
& lt; Input Data - val = "true" id = "QuizType" name = "QuizType" type = "hidden" value = "1000" & gt; // binder will force this just fine quiz type = 1000
, where it comes to the EnumDataType
validation feature. If I add the verification attribute for my attribute within my view Model: [EnumDataType (typeof (QuizType), ErrorMessage = "Quiz type value does not exist within enum")] Public QuizType QuizType {get; In the set;}
With the specialty in place, I would only be able to specify my valid enum values (0 or 1 for this example).
If incorrect, the STRING statements posted from the HTML are automatically valid for you, but any integer values will not be checked.
I hope that it validates ENUMS within ASP.NET MVC.
Comments
Post a Comment