In my .net core 2.0 Web API I am using EnumDataType() validation attribute on my model property. When the validation fails the custom error message is empty. I am not sure why it is happening -
[EnumDataType(typeof(MyEnum), ErrorMessage = "Custom Error Message")]
public MyEnum MyEnumProp {get; set;}
I checked the other properties where I have [Required], [MinLength] and all are generating custom error messages. Am I doing something wrong? Is there any other approach?
This lets you define an enumeration that contains descriptive values that correspond to database values, and then use the enumeration constant names instead of the database values when data is displayed. Initializes a new instance of the EnumDataTypeAttribute class. Gets the name of custom field template that is associated with the data field.
EnumDataType The value should match with one of the enum members. Open your visual studio and create a new empty ASP.NET MVC application. Follow ASP.NET MVC Model binding tutorial which gives you a basic mvc application with simple model binding. You can also Download source code of previous step for basic application.
The DataAnnotations validation attributes used with Customer model properties. Which will force validation on View. You can get more details on how ASP.NET MVC request life cycle works.
Follow ASP.NET MVC Model binding tutorial which gives you a basic mvc application with simple model binding. You can also Download source code of previous step for basic application. In this step, you will add DataAnnotation attributes to the Customer model. Open the Customer.cs file from the Models folder.
It's a frequent confusion between errors detected during deserialization and validation stages.
Say you have the following enum
:
public enum MyEnum
{
None,
Value1,
Value2
}
and the following model:
public class TestModel
{
[Required]
public int? Id { get; set; }
[EnumDataType(typeof(MyEnum), ErrorMessage = "Custom Error Message")]
public MyEnum MyEnumProp { get; set; }
}
When you post the data:
{
"Id": 123,
"MyEnumProp": "UnexistingEnumValue"
}
the error will happen during deserialization stage (in Json.NET for this case). Deserializer just has no way to convert string "UnexistingEnumValue"
to some of values from MyEnum
.
In this case deserializer will register following model binding error: Requested value 'UnexistingEnumValue' was not found
.
ModelState.IsValid
will be set to false
, however value of MyEnumProp
will be left at its default value of MyEnum.None
. Validation performed by EnumDataType
attribute will not detect any error, because MyEnum.None
is a valid value for MyEnum
. That is why you will not see "Custom Error Message"
in ModelState
errors.
Now if you post the following data:
{
"Id": 123,
"MyEnumProp": 5
}
No error will happen during deserialization stage, because following assignment is pretty legal even if it does not make much sense:
MyEnum v = (MyEnum)5;
So deserializer will not detect any errors. However now EnumDataType
validation comes into play. And it detects that 5
is not a valid value for MyEnum
. ModelState.IsValid
is set to false
and the error message specified in EnumDataType.ErrorMessage
is registered ("Custom Error Message"
).
If you want to have the same custom message for both deserialization and validation errors, you should go up to the level of deserializer (Json.NET) and use its extensibility points for this purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With