Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve the string value of a custom C# resource class constant?
    primarykey
    data
    text
    <p>I have created the following custom ValidationAttribute: </p> <pre><code>public class DateRangeAttribute : ValidationAttribute, IClientValidatable { public DateTime MinimumDate = new DateTime(1901, 1, 1); public DateTime MaximumDate = new DateTime(2099, 12, 31); public DateRangeAttribute(string minDate, string maxDate, string errorMessage) { MinimumDate = DateTime.Parse(minDate); MaximumDate = DateTime.Parse(maxDate); ErrorMessage = string.Format(errorMessage, MinimumDate.ToString("MM/dd/yyyy"), MaximumDate.ToString("MM/dd/yyyy")); } } </code></pre> <p>that I would like to use in my MVC4 view model as follows:</p> <pre><code>[DateRange(Resources.MinimumDate, Resources.MaximumDate, "Please enter a date between {0} and {1}")] </code></pre> <p><em>Resources</em> is a generated resources class based on a set of options stored in a SQL database. A simplified version of the generated code for the above two resource properties is:</p> <pre><code>public class Resources { public const string MinimumDate = "PropMinimumDate"; public static string PropMinimumDate { get { return "12/15/2010" } } public const string MaximumDate = "PropMaximumDate"; public static string PropMaximumDate { get { return "12/15/2012" } } } </code></pre> <p>While I do not understand how it works, I do understand that typical usage of resources in ValidationAttributes will automatically map <em>Resources.MinimumDate</em> to <em>PropMinimumDate</em> and return the value "12/15/2010".</p> <p>What I am cannot figure out is how to manually make that programmatic leap myself so I can pass in the two date values into my custom ValidatorAttribute. As presently coded, <em>"PropMinimumDate"</em> and <em>"PropMaximumDate"</em> are the <em>minDate</em> and <em>maxDate</em> parameter values (respectively) passed into the constructor of the DateRangeAttribute.</p> <p>If I try</p> <pre><code>[DateRange(Resources.PropMinimumDate, Resources.PropMaximumDate, "Please enter a date between {0} and {1}")] </code></pre> <p>I receive the compile error: </p> <p><em>An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type</em></p> <p>Is there a way to accomplish this task, or am I attempting the impossible? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload