2011-11-26 139 views
1

我嘗試使用This Way火在客戶端自定義驗證但無法正常工作:ASP.NET MVC運行自定義驗證程序在客戶端

這是我的模型,我的驗證:

public class ExactStringAttribute : ValidationAttribute { 

    public string ValidName { get; set; } 

    public override bool IsValid(object value) { 

     if (value.ToString() == ValidName) 
      return true; 
     else 
      return false; 
    } 
} 

public class Teacher { 

    public long Id { get; set; } 
    public string Name { get; set; } 
    public string Family { get; set; } 
    public int Age { get; set; } 
    [Display(Name = "SchoolName CustomValidator(ValidName='MySchool')")] 
    [ExactString(ValidName = "MySchool", ErrorMessage = "The Input Name must 
     exactly equal by Valid Name")] 
    public string SchoolName { get; set; } 
} 

這是我的腳本:

<script type="text/javascript"> 
Sys.Mvc.ValidatorRegistry.validators["exactstring"] = function (rule) { 
    // initialization code can go here. 
    var validname = rule.ValidationParameters["validname"]; 

    // we return the function that actually does the validation 
    return function (value, context) { 
     if (value == validname) { 
      return true; /* success */ 
     } 
     return rule.ErrorMessage; 
    }; 
}; 
</script> 

和Global.asax中的一部分:

protected void Application_Start() { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(ExactStringAttribute), 
typeof(ExactStringValidator)); 
    } 




    public class ExactStringValidator : 
DataAnnotationsModelValidator<ExactStringAttribute> { 

     string validName; 
     string errorMessage; 

     public ExactStringValidator(ModelMetadata metadata, 
ControllerContext context 
      , ExactStringAttribute attribute) 
      : base(metadata, context, attribute) { 
      validName = attribute.ValidName; 

     } 

     public override IEnumerable<ModelClientValidationRule> 
     GetClientValidationRules() { 
      var rule = new ModelClientValidationRule { 
       ErrorMessage = errorMessage, 

       //Must be lowercase 
       ValidationType = "exactstring" 

      }; 

      //Must be lowercase 
      rule.ValidationParameters.Add("validname", validName); 

      return new[] { rule }; 
     } 
    } 

而且我_layout頁添加腳本:

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"> 
</script> 
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript">  
</script> 
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"> 
</script> 
<script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" 
type="text/javascript"></script> 

控制器的一部分:

[HandleError] 
public class TeacherController : Controller { 
...... 
} 

當我添加在視圖頁面Html.EnableClientValidation();表示有關說法,但在web.config中的錯誤文件添加了以下設置:

<appSettings> 
    <add key="webpages:Version" value="1.0.0.0"/> 
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings> 

但不幸的是驗證器未在客戶端觸發T方面,有沒有人知道這個呢?

回答

3

如果你看一下這個答案使用的方法:

MVC custom validation: compare two dates

,你會看到你的驗證器組件的變化,你應該做,以及您必須添加什麼。

+0

這對我不起作用,我不知道問題在哪裏,我在這裏問:http://stackoverflow.com/questions/8284207/asp-net-mvc-implement-custom-validator-use- iclientvalidatable – Saeid