2016-11-01 18 views
-1

私は現在、ASP.NET MVCでWebプロジェクトを開発中です。私はそれにページを作成し、それはSQL Serverを介してデータベーステーブルに対応するエンティティを作成します。必要なタグだけではなく、カスタムバリデーションクラスを使用することを選択しました。私は完全に動作する私の他のカスタムバリデーションクラスとまったく同じようにクラスをコーディングしましたが、何らかの理由でこのクラスは私が何を試みてもページを送信しても発砲しません。カスタムクラスの検証が機能していませんか?

のViewModel:

public class MyViewModel { 
    //my other properties   

     [ValidateCreateDate(ErrorMessage = "Date must be after or on today's date.")] 
     public DateTime? RequestedDate { get; set; } 
} 

クラス:

public class ValidateCreateDateAttribute : RequiredAttribute 
{ 
    private context db = new context(); 

    public override bool IsValid(object value) //breakpoint is never hit here 
    { 
     //code to check if it is valid or not 
    } 
} 

ビュー:

@model context.MyViewModel 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
@using (Html.BeginForm("Create", "Data", FormMethod.Post)) 
{ 
    <div class="row">  
    <div class="col-md-2"> 
     @Html.EditorFor(model => model.RequestedDate) 
    </div> 
    <div class="col-md-2"> 
     @Html.ValidationMessageFor(model => model.RequestedDate) 
    </div> 
    <div class="col-md-8"> 
     <button type="submit">Submit</button> 
    </div> 
</div> 
    } 

<script type="text/javascript"> 
    $(function() { 
     $("#RequestedDate").datepicker(); 
    }); 
</script> 
+0

ClientValidationEnabledとUnobtrusiveJavaScriptEnabledを、js-を含みます邪魔にならないようにファイル? – Mackan

+0

はい、両方とも有効になっていて、実際のコードに邪魔にならないファイルを添付しました。ありがとうございます@Mackan –

+0

@EliHellmerフォームが正しく送信されていますか?それは "作成"アクションメソッドに当たっていますか?あなたのコードはうまく見えますが – Shyju

答えて

0

コメントは

のModを推奨していますのようにしてくださいValidationAttributeないRequiredAttribute導出https://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

検証の実装:elState.IsValid

、こことしてエラーを返すがどのように説明している設定で

namespace YourProject.Common.DataAnnotations 
    { 
     [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)] 
     public sealed class ValidateCreateDateAttribute : ValidationAttribute 
     { 
      private const string _defaultErrorMessage = "Date is requierd, value most be after or on today's date."; 
      public ValidateCreateDateAttribute() 
      { 
       if (string.IsNullOrEmpty(ErrorMessage)) 
       { 
        ErrorMessage = _defaultErrorMessage; 
       } 
      } 
      protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
      { 
       if (value != null) 
       { 
        Type type = value.GetType(); 
        if (type.IsPrimitive && (type == typeof(DateTime) || type == typeof(DateTime?))) 
        { 
         var checkDate = Convert.ToDateTime(value); 
         //in UTC, if you are using timezones, use user context timezone 
         var today = DateTime.UtcNow.Date; 
         //compare datetimes 
         if (DateTime.Compare(checkDate, today) < 0) 
         { 
          return new ValidationResult(ErrorMessage); 
         } 
         //success 
         return ValidationResult.Success; 
        } 
return new ValidationResult("Cannot validate a non datetime value"); 
       } 
       //if value cannot be null, you are using nullable date time witch is a paradox 
       return new ValidationResult(ErrorMessage); 
      } 
     } 
    } 
+0

ありがとうございました! @SilentTremor –

関連する問題