Jquery 为什么使用自定义数据注释属性进行不引人注目的客户端验证不会';不行?
我已经创建了一个简单的属性来检查选择了多少个选项。 我在js中添加了适配器和验证方法,但验证仍然不起作用Jquery 为什么使用自定义数据注释属性进行不引人注目的客户端验证不会';不行?,jquery,asp.net-mvc-5,unobtrusive-validation,Jquery,Asp.net Mvc 5,Unobtrusive Validation,我已经创建了一个简单的属性来检查选择了多少个选项。 我在js中添加了适配器和验证方法,但验证仍然不起作用 using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace MicroSpecShow.WebAdmin.AdminCore.Attributes {
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MicroSpecShow.WebAdmin.AdminCore.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
sealed public class AtLeastChecked : ValidationAttribute, IClientValidatable
{
public int Min { get; set; }
public AtLeastChecked(int min)
{
Min = min;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var list = value as ICollection;
string message;
if (list != null)
{
var count = list.Count;
if (count < Min)
{
message = string.Format(ErrorMessageString, Min);
return new ValidationResult(message);
}
}
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = ErrorMessageString;
rule.ValidationType = "atleastchecked";
rule.ValidationParameters.Add("min", Min);
yield return rule;
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Web.Mvc;
命名空间MicroSpecShow.WebAdmin.AdminCore.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,AllowMultiple=false)]
密封的公共类至少已选中:ValidationAttribute,IClientValidable
{
公共int Min{get;set;}
公共至少已检查(整数分钟)
{
Min=Min;
}
受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext)
{
var list=作为ICollection的值;
字符串消息;
如果(列表!=null)
{
var count=list.count;
如果(计数<分钟)
{
message=string.Format(ErrorMessageString,最小值);
返回新的ValidationResult(消息);
}
}
返回ValidationResult.Success;
}
公共IEnumerable GetClientValidationRules(ModelMetadata元数据、ControllerContext上下文)
{
var rule=new ModelClientValidationRule();
rule.ErrorMessage=ErrorMessageString;
rule.ValidationType=“atleastchecked”;
规则.ValidationParameters.Add(“min”,min);
收益率-收益率规则;
}
}
}
我的js
$(函数(){
ShowGeneral.init();
});
(函数(jQuery){
log(jQuery);
jQuery.validator.unobtrusive.adapters.add('atleastchecked',['min'],函数(选项){
options.rules['atleastchecked']=options.params.min;
options.messages['atleastchecked']=options.message;
});
jQuery.validator.addMethod('atleastchecked',函数(值、元素、最小值){
if(typeof$(element).val()=='undefined'| |$(element).val()==null){
返回false;
}
if($(元素).val().length
剃刀:
<div class="col-md-6">
@Html.ListBoxFor(m => m.SelectedAllowedLanguages, Model.AllLanguages, new { multiple = "multiple", @class = "show-general-form-control" })
</div>
@Html.ListBoxFor(m=>m.SelectedAllowedLanguages,Model.AllLanguages,new{multiple=“multiple”,@class=“show general form control”})
生成的HTML结构:
<div class="col-md-6">
<select class="show-general-form-control" data-val="true" data-val-atleastchecked="You must check at least {0} language." data-val-atleastchecked-min="2" id="SelectedAllowedLanguages" multiple="multiple" name="SelectedAllowedLanguages" style="display: none;">
<option selected="selected" value="1">English</option>
<option selected="selected" value="2">French</option>
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 516px;"><span class="ui-icon ui-icon-triangle-2-n-s"></span><span>2 selected</span></button>
</div>
英语
法语
2选定
我的应用程序设置:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
为什么验证不起作用以及如何解决此问题?您的web.config的“appSettings”部分是否有以下内容:是的,我已经更新了帖子
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />