MVC3: How to change the generic [Required] validation message text?

前端 未结 2 1791
温柔的废话
温柔的废话 2021-02-15 12:23

When you decorate a model object\'s property with the Required attribute and don\'t specify ErrorMessage or ResourceType/Name you get the

2条回答
  •  广开言路
    2021-02-15 12:53

    Have you tried creating a derived class of RequiredAttribute and overriding the FormatErrorMessage method? This should work:

    public class MyRequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
    {
        public override string FormatErrorMessage(string name)
        {
            return base.FormatErrorMessage(string.Format("This is my error for {0}", name));
        }
    }
    

提交回复
热议问题