2017-02-20 17 views
-1

我正在使用MVC中的asp.net工作。现在我必须为每个输入提供验证。因为我是MVC的新手,对于如何提供验证并不太了解。任何人都可以帮助我得到这个。ASP.net MVC应用程序中的数据验证

上传

在上述的一段代码接受最大日期值和最小数据值。 它应该首先接受最短日期(发布日期)值和最大日期(到期日期)秒,但现在它的工作正在进行。 任何人都可以帮助我为此提供验证。

回答

0

在这里你可以设置双向验证 一个是客户端,另一个是服务器端低于

服务器端验证给您可以比较当前日期/时间的发行日期,也比较到期日与发布日期

public class MyClass : IValidatableObject 
{    
    [Required(ErrorMessage="issued date and time cannot be empty")] 
    //validate:Must be greater than current date 
    [DataType(DataType.DateTime)] 
    public DateTime issuedDateTime { get; set; } 

    [Required(ErrorMessage="expiry date and time cannot be empty")] 
    //validate:must be greater than issuedDate 
    [DataType(DataType.DateTime)] 
    public DateTime expiryDateTime { get; set; }  

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
     List<ValidationResult> results = new List<ValidationResult>(); 

     if (issuedDateTime < DateTime.Now) 
     { 
      results.Add(new ValidationResult("issued date and time must be greater than current time", new []{"issuedDateTime"})); 
     } 

     if (expiryDateTime <= issuedDateTime) 
     { 
      results.Add(new ValidationResult("expiryDateTime must be greater that issuedDateTime", new [] {"expiryDateTime"})); 
     } 

     return results; 
    }  
} 

我希望它能帮助你。

相关问题