嗨,我已经找到了这个答案: MVC3 Validation - Require One From Group
这对于检查组名并使用反射是相当具体的。
我的例子可能有点简单,我只是想知道是否有更简单的方法来做到这一点。
我有以下内容:
public class TimeInMinutesViewModel {
private const short MINUTES_OR_SECONDS_MULTIPLIER = 60;
//public string Label { get; set; }
[Range(0,24, ErrorMessage = "Hours should be from 0 to 24")]
public short Hours { get; set; }
[Range(0,59, ErrorMessage = "Minutes should be from 0 to 59")]
public short Minutes { get; set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
public short TimeInMinutes() {
// total minutes should not be negative
if (Hours <= 0 && Minutes <= 0) {
return 0;
}
// multiplier operater treats the right hand side as an int not a short int
// so I am casting the result to a short even though both properties are already short int
return (short)((Hours * MINUTES_OR_SECONDS_MULTIPLIER) + (Minutes * MINUTES_OR_SECONDS_MULTIPLIER));
}
}
我想向 Hours & Minutes 属性或类本身添加一个验证属性。想法是确保这些属性中的至少 1 个(小时或分钟)具有值,服务器和客户端验证使用自定义验证属性。
请问有人有这方面的例子吗?
谢谢