asp.net-mvc – ASP.NET MVC中的动态范围验证2
发布时间:2020-05-24 06:26:15 所属栏目:asp.Net 来源:互联网
导读:我正在使用ASP.NET MVC2,并尝试使用System.ComponentModel.DataAnnotations命名空间中的属性验证我的视图模型. 如何动态设置RangeAttribute的允许有效范围? 例如,如果我想验证输入的日期是否在预期范围内. 这不编译: [Range(typeof(DateTime), DateTime.Tod
|
我正在使用ASP.NET MVC2,并尝试使用System.ComponentModel.DataAnnotations命名空间中的属性验证我的视图模型. 如何动态设置RangeAttribute的允许有效范围? 这不编译: [Range(typeof(DateTime),DateTime.Today.ToShortDateString(),DateTime.Today.AddYears(1).ToShortDateString())]
public DateTime DeliveryDate { get; set; }
因为“属性参数必须是一个常量表达式,typeof表达式或数组创建表达式的属性参数类型”. 我需要诉诸创建我自己的自定义验证器吗? 解决方法好的,找到答案. .NET Framework 4提供了一个新的CustomValidationAttribute,这使得以下可能:[Required]
[DisplayName("Ideal Delivery Date")]
[CustomValidation(typeof(HeaderViewModel),"ValidateDeliveryDate")]
public DateTime DeliveryDate { get; set; }
public static ValidationResult ValidateDeliveryDate(DateTime deliveryDateToValidate)
{
if (deliveryDateToValidate.Date < DateTime.Today)
{
return new ValidationResult("Delivery Date cannot be in the past.");
}
if (deliveryDateToValidate.Date > DateTime.Today.AddYears(1))
{
return new ValidationResult("Delivery Date must be within the next year.");
}
return ValidationResult.Success;
}
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28VS.100%29.aspx (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- ide – 我如何处理必须编写经典ASP的代码?
- asp.net-mvc-4 – ASP.NET MVC 4 ScriptBundle返
- asp.net-core – 重新挑战ASP.NET Core中经过身份
- asp.net-mvc – 在Razor VB.net中使用MVC无法按预
- asp.net url分页类代码
- asp.net – 如何配置IIS以便在连接到SQL Server时
- asp.net-web-api – WebApi:如何将状态从过滤器
- asp.net – Intranet / Internet的Windows身份验
- asp.net-core – 如何使用FluentValidation.AspN
- asp.net – 如何添加两个CSS类来控制代码背后?
热点阅读
