2011-08-24 75 views
0

我已创建自定义的验证属性客户端自定义与属性validaton不起作用

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 
    public sealed class ValidateDublicateNameAttribute : ValidationAttribute, IClientValidatable 
    { 
     private const string _defaultErrorMessage = "Library with title {0} is already exist"; 
     private UnitOfWork unit = new UnitOfWork(); 

     public ValidateDublicateNameAttribute() 
      : base(_defaultErrorMessage) 
     { 
     } 
     public override s 

特林FormatErrorMessage(字符串名称){ 回报 的String.Format(ErrorMessageString,姓名); }

protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     string valueAsString = value as string; 
     ValidationResult result = ValidationResult.Success; 
     if (String.IsNullOrEmpty(valueAsString)) 
     { 
      if (unit.ResourceSrvc.GetLibraryByTitle(valueAsString) != null) 
      { 
       result = new ValidationResult(String.Format(_defaultErrorMessage,value)); 
      } 
     } 
     else 
     { 
      result = new ValidationResult("Title cant be empty or null"); 
     } 
     return result; 
    } 

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     var rule = new ModelClientValidationRule 
     { 
      ErrorMessage = this.ErrorMessage, 
      ValidationType = "dublicatename", 
     }; 
     yield return rule; 
    } 
} 

,并用它

public class ResourceLibraryModel 
{ 
    public Guid LibraryId { get; set; } 
    [Required] 
    [ValidateDublicateName(ErrorMessage="title cant dublicate")] 
    public string Title { get; set; } 
} 

在客户端装饰了我的模型我有

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 

     $.validator.addMethod("dublicatename", function (value, element, params) { 
      alert("test"); 
     }); 

    }); 
</script> 

我没有任何验证的参数,并没有写$ .validator。 unobtrusive.adapters.add(....)

服务器端验证完美,但cli恩方不起作用。

任何想法?

,这是鉴于

 @using (Ajax.BeginForm("CreateLibrary", "Resource", new AjaxOptions { OnSuccess = "RequestSucceeded"})) 
     { 
      @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") 
       <div> 
        @*<div style="margin-bottom:15px"> 
         <label><b>Library information:</b></label> 
        </div>*@ 

        <div class="editor-field"> 
         @Html.TextBoxFor(m => m.Title, new { @class = "logon-field" }) 
         @Html.ValidationMessageFor(m => m.Title) 
        </div> 
        <div class="editor-label"> 
         @Html.LabelFor(m => m.Title) 
        </div> 
       </div> 
} 
+0

什么是您的看法是什么样子? – link664

+0

我编辑的问题,现在它包含在网络配置的视图 –

回答

0

我认为这个问题是jquery.validate.min.js必须在JavaScript代码后加载中...

0

只是一个猜测,但你能在你的Web客户端验证.config文件?

<appSettings> 
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings> 

如果是按照这个链接启用客户端验证部分:http://www.asp.net/mvc/tutorials/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript

+0

好吧,并需要验证工作, –

+0

对不起,我不太明白。这是否意味着你有工作?内置验证的 – link664

+0

正在工作,但我的自定义验证不起作用 –