2016-11-01 181 views
-1

我目前正在用ASP.NET MVC開發一個Web項目。我有一個創建頁面,它通過SQL服務器創建一個對應於數據庫表的實體。我需要一些驗證,而不僅僅是'required'標記,所以我選擇使用自定義驗證類。我編寫的類完全像我的其他自定義驗證類,完美的工作,但由於某種原因,這個類沒有射擊頁面提交,無論我嘗試。自定義類驗證不起作用?

視圖模型:

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

做一個評論建議

國防部elState.IsValid

和這裏返回一個錯誤是講解如何:https://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api

驗證實現派生ValidationAttribute沒有RequiredAttribute標籤

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 –