I want to know, is there a way that I can pass an object(let’s say a Message instance) from the validation in the model’s fields and retrieve them from the ModelState( or by using some other thing) instance. Why I’m asking this is, I want to differentiate between the validation error message types so I can display only the messages I want in the view at a time. (Ex : required messages shown before the unique validation messages.)
I was trying to use a custom created Message object which I can then distinguish using its messageType field. But as the validation only returns string messages, can’t think of a way.
Are you ok with reading the messages out of ModelState and determining type based on message content? That can be done if you set a custom message for all the validations.
Then you can evaluate each message looking for specific content and take action. Such as putting the word “Error” in the
Requiredattribute andInfoin the rest.Here’s a class you can use to test
Model
Controller
View