Asp.net mvc 2 向MVC2中的自定义数据注释属性显示自定义客户端错误消息
我在MVC2中创建了一个名为remoteVal的自定义远程数据注释属性,如下所示Asp.net mvc 2 向MVC2中的自定义数据注释属性显示自定义客户端错误消息,asp.net-mvc-2,jquery-validate,data-annotations,Asp.net Mvc 2,Jquery Validate,Data Annotations,我在MVC2中创建了一个名为remoteVal的自定义远程数据注释属性,如下所示 public class remoteValAttribute:ValidationAttribute { public string Action { get; set; } public string Controller { get; set; } public string ParameterName { get; set; } public string RouteName
public class remoteValAttribute:ValidationAttribute
{
public string Action { get; set; }
public string Controller { get; set; }
public string ParameterName { get; set; }
public string RouteName { get; set; }
public override bool IsValid(object value)
{
return true;
}
}
适配器类
public class RemoteAttributeAdapter:DataAnnotationsModelValidator<remotevalAttribute>
{
public RemoteAttributeAdapter(ModelMetadata metadata, ControllerContext context, remoteVal attribute) : base(metadata, context, attribute) { }
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
ModelClientValidationRule rule = new ModelClientValidationRule()
{
ErrorMessage = ErrorMessage,
ValidationType = "remoteVal"
};
rule.ValidationParameters["url"] = GetUrl();
rule.ValidationParameters["parameterName"] = Attribute.ParameterName;
return new ModelClientValidationRule[] { rule };
}
private string GetUrl()
{
RouteValueDictionary rvd = new RouteValueDictionary(){
{"controller",Attribute.Controller},
{"action",Attribute.Action}
};
var virtualPath = RouteTable.Routes.GetVirtualPath(ControllerContext.RequestContext, Attribute.RouteName, rvd);
if (virtualPath == null)
{
throw new InvalidOperationException("No route matched!");
}
return virtualPath.VirtualPath;
}
}
添加jQuery脚本如下
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMvcJQueryValidation.js"></script>
<script type="text/javascript">
$(function () {
var ermessage = '';
jQuery.validator.addMethod("remoteVal", function (value, element, params) {
var validator = this;
var valid = false;
if (this.optional(element)) {
return true;
}
else {
var url = params.url;
var parameterName = params.parameterName;
var newUrl = ((url.indexOf('?') < 0) ? (url + '?') : (url + '&'))
+ encodeURIComponent(parameterName) + '=' + encodeURIComponent(value);
var response = $.ajax({
url: newUrl,
async: false
}).responseText;
if (response == 'OK')
valid = true;
else {
valid = false;
var errors = {};
errors[element.name] = response;
validator.showErrors(errors);
}
}
return valid;
});
});
</script>
$(函数(){
var-ermessage='';
jQuery.validator.addMethod(“remoteVal”,函数(值、元素、参数){
var验证器=此;
var-valid=false;
if(此.可选(元素)){
返回true;
}
否则{
var url=params.url;
var parameterName=params.parameterName;
var newUrl=((url.indexOf('?')<0)?(url+'?'):(url+'&'))
+encodeURIComponent(参数名称)+'='+encodeURIComponent(值);
var响应=$.ajax({
url:newUrl,
异步:false
}).responseText;
如果(响应=‘确定’)
有效=真;
否则{
有效=错误;
var错误={};
错误[element.name]=响应;
验证程序错误(错误);
}
}
返回有效;
});
});
如果标题已经注册,来自AJAX调用的响应将显示为错误消息。但是错误总是显示为字段标题无效。请帮我解决这个问题
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMvcJQueryValidation.js"></script>
<script type="text/javascript">
$(function () {
var ermessage = '';
jQuery.validator.addMethod("remoteVal", function (value, element, params) {
var validator = this;
var valid = false;
if (this.optional(element)) {
return true;
}
else {
var url = params.url;
var parameterName = params.parameterName;
var newUrl = ((url.indexOf('?') < 0) ? (url + '?') : (url + '&'))
+ encodeURIComponent(parameterName) + '=' + encodeURIComponent(value);
var response = $.ajax({
url: newUrl,
async: false
}).responseText;
if (response == 'OK')
valid = true;
else {
valid = false;
var errors = {};
errors[element.name] = response;
validator.showErrors(errors);
}
}
return valid;
});
});
</script>