Question

Error message using Data Annotations but in ErrorMessage I want to pass variable string but when I do that it gives error when project is build.

string errorMessage="Something happened";

[Remote("IsTimeValid", "Account", AdditionalFields = "TaskDate,TodoID", ErrorMessage =errorMessage)]
public string Time{get;set;}

Is there any way to show variable value in ErrorMessages?

Was it helpful?

Solution

You have the ability to do pretty much exactly what you're asking (in your comment).

You set ErrorMessageResourceType to be the type which contains the static property and you set ErrorMessageResourceName to be the name of the property on the class

E.g.

public class Time
{
    [Remote("IsTimeValid", "Account", AdditionalFields = "TaskDate,TodoID", ErrorMessageResourceType = typeof(Time), ErrorMessageResourceName = "InvalidTimeZoneMessage")]
    public string Time{get;set;}

    public static string InvalidTimeZoneMessage
    {
        get
        {
            return "You have the wrong timezone";
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top