2

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?

4
  • @Ehsan can you please answer my query.? Commented May 8, 2014 at 6:57
  • i think its not possible, but may be possible i never did something like this Commented May 8, 2014 at 6:59
  • You can only assign constant values to attribute properties so what you're attempting is not possible. Why are you trying to set the error message from the variable? there might be another way to achieve the same thing Commented May 8, 2014 at 7:05
  • @Martin i want to convey my user that this error is according to its timezone.. Basically static method from baemodel return string for which timezone user try to insert date. I want that static method in place of ErrorMessage Commented May 8, 2014 at 10:01

1 Answer 1

4

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";
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. I am using const string field but not succeeeded with that. It is pretty nice Answer :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.