2

Scenario :- I am developing MVC 4 application, the website will run in several languages and will be hosted on Azure. For localizing we are relying on the database rather than resource bundle approach.

Problem :- I want to customize error messages at runtime, I want to localize the messages through the database.

I have tried to change attribute values through reflection but it had not worked.

Code :-

     //Model
      public class Home
      {
          [Required(ErrorMessage = "Hard coded error msg")]
           public string LogoutLabel { get; set; }
      } 

     //On controller
      public ActionResult Index()
    {
        Home homeData = new Home();
        foreach (PropertyInfo prop in homeData.GetType().GetProperties())
        {
            foreach (Attribute attribute in prop.GetCustomAttributes(false))
            {

               RequiredAttribute rerd = attribute as RequiredAttribute;

                if (rerd != null)
                {
                    rerd.ErrorMessage = "dynamic message";
                }
            }


        }

        return View(homeData);
    }  

On client side when validation takes place it shows me old message "Hard Coded error msg". Please suggest how this can be customised if we donot want to use Resource bundle approach

1

2 Answers 2

1

You would better create and register your own DataAnnotationsModelMetadataProvider where you can just override the error messages. For more detail please see the answers to the similar question here MVC Validation Error Messages not hardcoded in Attributes

1

are you intended to implement this nested loop to localize validation message of all you'r entities ? i think no.a better solution is using Validator attribute.

for you'r class :

 [Validator(typeof(HomeValidator))]
 public class Home
      {              
           public string LogoutLabel { get; set; }
      }

now lets implement HomeValidator :

public class HomeValidator : AbstractValidator<Home>
    {
        public HomeValidator()
        {
            RuleFor(x => x.LogoutLabel ).NotEmpty().WithMessage("your localized message");
        }
    }
4
  • But in this case i have to write rules for the available validators. Does only changing the messages are not possible?
    – Deepesh
    Dec 20, 2012 at 7:55
  • then only change message with : RuleFor(x => x.LogoutLabel ).WithMessage("your localized message"); Dec 20, 2012 at 8:01
  • seems interesting let me check, so in this case validation for empty or others will work at it is like if model has multiple properties and each property having different validation annotation, then also this solution will work?
    – Deepesh
    Dec 20, 2012 at 8:22
  • what will happen when one property have multiple validation, plus in this approach we have to explicitly tackle every property can't we extend attributes so it can be done in more genralised way?
    – Deepesh
    Dec 20, 2012 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.