问题
I'm writing a Visual Studio extension. I have a C# class representing an options page. An option is represented by a public property, like this:
public class OptionsPageGeneral : DialogPage
{
[Range(1, 100)]
[Category("Misc")]
[DisplayName("Integer option")]
public string IntOption { get; set; }
...
}
I'm trying to use the RangeAttribute validation attribute to restrict user input. However, for the given code, user still can input any value, not only from [1; 100] range.
I've seen lots examples of ValidationAttribute usage, but all for ASP.NET MVC projects. Is it true this attribute is only applicable in that context?
Anyway, how can I validate user input done in options page? I know I can simply override the property set
method, but validation attributes require much less code to write and can be reused for the similar properties.
回答1:
I dont have idea on how to use options page, but you can use a regular expression if you want a range between 1-100 instead of Range
.
[Category("Misc")]
[DisplayName("Integer option")]
[RegularExpression(@"[0-9]{2}")]//This will only allow you to enter only 2 digits
public string IntOption { get; set; }
来源:https://stackoverflow.com/questions/12044509/how-to-use-validationattribute-for-user-input-in-options-pages