I created an inherited attribute like this in ASP.NET MVC3:
public sealed class RequiredFromResourceAttribute : RequiredAttribute
{
public RequiredFromResourceAttribute(string errorResourceName, string errorResourceTypeName)
{
this.ErrorMessageResourceName = errorResourceName;
this.ErrorMessageResourceType = Type.GetType(errorResourceTypeName);
}
}
And use it like this:
[RequiredFromResource("Title", "Resources.Resource, MyProject.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
public string Title { get; set; }
It didn’t work and the MVC ignored it. Then I create a simpler class which just inherited from RequiredAttribute like this:
public class MyRequiredAttribute : RequiredAttribute
{
}
I use it like that I said. But it didn’t work again.
Although, all these ways work on “DisplayNameAtrribute” perfectly.
What is the problem?
You can fix this by adding the following code in Global.asax: (found the answer here)
Alternatively, using marcind’s solution, I found that the constructor for
ModelClientValidationRequiredRulerequires an error message. Here is an updated version that includes the display name for the field: