2012-05-28 43 views
1

我一直在ASP.Net MVC3上试图让客户端自定义验证工作在最近两天。我已经按照例子here,here,herehere,但不能让它运行。可能有人来看看我(目前)代码,并请让我知道,如果你看到任何错误,任何帮助都将不胜感激mvc3 jquery客户端自定义验证没有运行

Web.Config中既有ClientValidationEnabled & UnobtrusiveJavaScriptEnabled设置为true

_layout。 CSHTML引用文件如下

<script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/DateFormatValidator.js")" type="text/javascript"></script> 

服务器端验证类被定义如下

public class DateFormatValidation : ValidationAttribute, IClientValidatable 
{ 
    public string ValidDateFormat { get; set; } 

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     var modelClientValidationRule = new ModelClientValidationRule 
     { 
      ValidationType = "validatedate", // the name of the validation rule as specified in DateFormatValidator.js 
      ErrorMessage = LocalisationStrings.InvalidDateFormat 
     }; 

     modelClientValidationRule.ValidationParameters["name"] = ValidDateFormat; 
     yield return modelClientValidationRule; 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    {    
     var dateString = (string) value; 
     if(String.IsNullOrEmpty(dateString)) 
     { 
      return ValidationResult.Success; 
     } 

     // if we can't convert to a date based on the current culture then we will get a null back from the ConvertLocalDateFormatToISO extension method 
     if(String.IsNullOrEmpty(dateString.ConvertLocalDateFormatToISO())) 
     { 
      return new ValidationResult(LocalisationStrings.InvalidDateFormat); 
     } 
     return ValidationResult.Success; 
    } 
} 

视图模型定义如下

public class SearchViewModel : ElectronicDocument 
{ 

    #region Properties 

    [DateFormatValidation(ValidDateFormat="validatedate")] // addig the paramter to the attribute is just a test to get this to work 
    public string DueDateFrom 
    { 
     get;    
     set;    
    } 

    [DateFormatValidation] 
    public string DueDateTo 
    { 
     get; 
     set;    
    } 

的客户端验证脚本如下:

(function ($) { 
$.validator.addMethod('validatedateformat', function (value, element, param) { 
    if (!value) { return true; } 
    try { 
     var isValidDate = false; 
     /* - CheckDateValidFormat is available from the base controller class */ 
     $.get('CheckDateValidFormat(' + value +')', 
      function (data) {     
       isValidDate = data; 
      }); 
     if (isValidDate) { 
      return true; 
     } 
     return false; 
    } 
    catch (e) { 
     return false; 
    } 
}); 

$.validator.unobtrusive.adapters.add('validatedate', ['name'], function (options) { 
    options.rules["validatedateformat"] = options.params.name; 
    if (options.message) options.messages["validatedateformat"] = options.message; 
}); 

}(jQuery的));

最后的观点如下所示:

<td style="font-size:10px; white-space:nowrap; color: #666">    
      @Html.TextBox("DocumentDateFrom", Model.DocumentDateFrom, new { @class = "date", style = "width:90px;" }) 
     to @Html.TextBox("DocumentDateTo", Model.DocumentDateTo, new { @class = "date", style = "width:90px;" })</td> 
+0

第一个'return false'应该在'客户端验证脚本'中吗?当然,它将永远无法到达下面的代码,因为它是无法访问的'尝试'开始? – SpaceBison

+0

对不起 - 我补充说,只是为了检查脚本是否正在运行,现在删除 - 仍然没有改进 – Johnv2020

+0

包含操作“CheckDateValidFormat”的代码在哪里,您可以看到它在您的开发IDE中被击中 – SpaceBison

回答

2

要包括jquery-1.8.11.min.js。这个版本的jQuery不存在,因为最新版本是1.7.2。

我怀疑你是包括jQuery UI的(最新版本1.8.20),你认为这是jQuery的。确保你使用的是正确的文件。

+0

谢谢Jakub - 我已经更新到1.7.2,但仍然没有去 – Johnv2020