I am using C# 4.5 and ASP.NET MVC 5. I have the following:
[Required(ErrorMessage = "Prop1 is required")]
public string Prop1 { get;set;}
[Required(ErrorMessage = "Prop2 is required")]
public string Prop2 { get;set;}
As you see the error message is the property name plus the " is required" string. What I need is instead of typing the property name and the message for every property, use a generic method composer, that will return the name of the decorated property and a string that I add, something like:
public string GetMessage()
{
// Caller property should be a variable that is retrieved dynamically
// that holds the name of the property that called the function
return CallerProperty + " is required";
}
so now I can user:
[Required(ErrorMessage = GetMessage())]
public string Prop2 { get;set;}
so in brief: How can I know the property name that is decorated by an attribute.
Aucun commentaire:
Enregistrer un commentaire