0

Hello I've got a model of USER and for example the property FIRSTNAME IS REQUIRED AND MUST BE BETWEEN 2 and 50 characters. That's why I use data annotation and the field FirstName in my class looks like :

public  class User
    {
       [Required(ErrorMessage = "{0}" + GETMESSAGEFROMDATABASE();)]
       [Range(typeof(int), "0", "100", ErrorMessage = "{0}" + GETMESSAGEFROMDATABASE();)]

        public string FirstName { get; set; }
        ...........................
   }

The problem is when I post the data and the Modelstate is not valid and I'm returned again in Register page I want to give custom error messages for each of the errors and the text should be get from a function; Please don't ask why I - just accept that I've got no other choice.

SO I want my validation summary to look like this :

*Name of the field : custom message for the error get from function

*Name of the field : custom message get from function

Tania Marinova
  • 1,788
  • 8
  • 39
  • 67
  • this post has the answer http://stackoverflow.com/questions/5857851/custom-validationsummary-template-asp-net-mvc-3 – Ashwini Verma Jun 05 '15 at 12:47
  • Cant you just handle this in your view model? You can drill into the error dictionary by doing ModelState.Errors.ContainsKey("FirstName") and then use that info to conditionally display whatever message you like. Maybe create a private method that takes the ModelState.Error dictionary and returns the list of custom error messages. – Brad C Jun 05 '15 at 15:10

0 Answers0