2017-02-20 15 views
-1

MVCでasp.netを使用して1つのアプリケーションで作業しています。今は、それぞれの入力ごとに検証を提供する必要があります。私はMVCに慣れていないので、バリデーションを提供する方法についてはあまり考えていません。誰でも私にこれを手伝うことができますか?ASP.net MVCアプリケーションのデータ検証

最大日付値と最小データ値を受け入れるコードの上記部分にアップロード

。 最初に最小の日付(発行日)の値を受け取り、最大の日付(有効期限)のsecond.butはその逆の動作を受け入れる必要があります。 誰でもこの妥当性確認の提供を手伝うことができます。

答えて

0

ここでは、双方向の検証 1を設定することができますが、クライアント側で、もう一つは、あなたが現在の日付/時間に対して発行された日付を比較しても比較することができ

の下に指定されたサーバ側

サーバー側の検証であります発行日の有効期限

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; 
    }  
} 

私はあなたに役立つことを願っています。