ASP.NET MVC运行自定义验证器的客户端 [英] ASP.NET MVC Run Custom Validators in Client Side
问题描述
我尝试使用这个方式火在客户端自定义验证但无法正常工作:
I try to use This Way to fire a custom validator in client side but that not work correctly:
这是我的模型,我的验证:
this is my model and my validator:
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中的一部分:
and a part of 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页添加脚本:
And Also I Add Script in _Layout Page:
<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>
控制器的一部分:
A part of Controller:
[HandleError]
public class TeacherController : Controller {
......
}
当我加入 Html.EnableClientValidation();
在表示有关说法,但在网页错误View页面。配置文件中的设置如下补充:
When I add Html.EnableClientValidation();
in view page that represent an error about argument but in web.config file the following setting added:
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
但不幸的是验证没有在客户端开火,没有任何一个知道这个紧靠
But unfortunately the validator didn't fire in client side, does any one know abut this?
推荐答案
如果你看一下这个答案使用的方法:
If you look at the methods used on this answer:
< A HREF =http://stackoverflow.com/questions/7025198/mvc3-custom-validation-compare-two-dates> MVC3自定义的验证:比较两个日期
你会发现你应该让你的验证器组件,以及您必须添加什么样的变化。
you will see the changes you should make to your validator components, as well as what you must add.
这篇关于ASP.NET MVC运行自定义验证器的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!