积极答复者
MVC模型验证问题

问题
答案
-
Range似乎是限定数值范围
刚刚找了一下资料,这问题跟楼主的很像
http://stackoverflow.com/questions/2334293/dynamic-range-validation-in-asp-net-mvc-2
所以代码改写一下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace MvcApplication1.Models { public class yourForm { [CustomValidation(typeof(MvcApplication1.Models.yourForm), "ValidateDate")] public DateTime OrderDate { get; set; } public static ValidationResult ValidateDate(DateTime deliveryDateToValidate) { if (deliveryDateToValidate.Date >= new DateTime(2000, 1, 1) && deliveryDateToValidate.Date <= new DateTime(2010, 1, 1)) { return ValidationResult.Success; } else { return new ValidationResult("Date must be between 2000/1/1 And 2010/1/1"); } } } }
这样应该就可以了
=====================
更正一下
应该用不着CustomValidation,楼主的代码我Run了一遍是正常的,会不会是您的Controller还是View的部份哪里写错?
Shadowと愉快なコード達
- 已标记为答案 Shiller_ 2011年8月16日 0:36
全部回复
-
Range似乎是限定数值范围
刚刚找了一下资料,这问题跟楼主的很像
http://stackoverflow.com/questions/2334293/dynamic-range-validation-in-asp-net-mvc-2
所以代码改写一下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace MvcApplication1.Models { public class yourForm { [CustomValidation(typeof(MvcApplication1.Models.yourForm), "ValidateDate")] public DateTime OrderDate { get; set; } public static ValidationResult ValidateDate(DateTime deliveryDateToValidate) { if (deliveryDateToValidate.Date >= new DateTime(2000, 1, 1) && deliveryDateToValidate.Date <= new DateTime(2010, 1, 1)) { return ValidationResult.Success; } else { return new ValidationResult("Date must be between 2000/1/1 And 2010/1/1"); } } } }
这样应该就可以了
=====================
更正一下
应该用不着CustomValidation,楼主的代码我Run了一遍是正常的,会不会是您的Controller还是View的部份哪里写错?
Shadowと愉快なコード達
- 已标记为答案 Shiller_ 2011年8月16日 0:36