无法在MVC3 </t>中对IEnumerable <t>进行客户端验证

时间:2012-12-01 22:15:02

标签: asp.net-mvc-3 unobtrusive-validation

我只是列出复选框以选择开发人员才华横溢的语言。我想验证必须选择至少一种语言。

服务器端验证没有失败。但是我无法让它在客户端工作。甚至“GetClientValidationRules”方法从未调用。这是我奇怪的想法。

客户端验证是否不支持IEnumerable?执行此操作的方法是什么?

ps:我不需要输入js验证码,因为我认为一旦调用了GetClientValidationRules就可以了

查看:(为简洁而简化)

public class DeveloperViewModel
{        
    [DeveloperLanguageSelectOneValidation(ErrorMessage="Please check at least one lang")]
    public IEnumerable<SelectListItem> LanguagesModel { get; set; }

}

验证:

public class DeveloperLanguageSelectOneValidationAttribute : ValidationAttribute ,IClientValidatable
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        IEnumerable<SelectListItem> items = (IEnumerable<SelectListItem>)value;
        foreach (var item in items)
            if (item.Selected)
                return ValidationResult.Success;
        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule() 
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "developerlanguageselectone"                        
        };

        return new[] { rule };
    }
}

SelectListItem查看:

@model SelectListItem
<li>
    @Html.HiddenFor(a => a.Value)
    @Html.CheckBoxFor(a => a.Selected)
    @Html.DisplayFor(a => a.Text)
</li>

开发者视图:

 <ul>
        @Html.EditorFor(a => a.LanguagesModel)</ul>
    @Html.ValidationMessageFor(a => a.LanguagesModel)

1 个答案:

答案 0 :(得分:0)

如何使用jquery?

$.validator.addMethod('require-one', function(value) {
    return $('.require-one:checked').size() > 0;
}, 'Please check at least one box.');

var checkboxes = $('.require-one');
var checkbox_names = $.map(checkboxes, function(e, i) {
    return $(e).attr("name")
}).join(" ");

$("#itemForm").validate({
    groups: {
        checks: checkbox_names
    },
    errorPlacement: function(error, element) {
        if (element.attr("type") == "checkbox") error.insertAfter(checkboxes.last());
        else error.insertAfter(element);
    }
});

您的表格:

<form name="itemForm" id="itemForm" method="post">

<label for="checkbox1">checkbox1</label>
<input checked="checked" data-val="true" data-val-required="The checkbox1 field is required." id="checkbox1" name="checkbox1" type="checkbox" value="true" class="require-one" />
<input name="checkbox1" type="hidden" value="false" /><br />

<label for="checkbox2">checkbox2</label>
<input checked="checked" data-val="true" data-val-required="The checkbox2 field is required." id="checkbox2" name="checkbox2" type="checkbox" value="true" class="require-one" />
<input name="checkbox2" type="hidden" value="false" /><br />

<label for="checkbox3">checkbox3</label>
<input checked="checked" data-val="true" data-val-required="The checkbox3 field is required." id="checkbox3" name="checkbox3" type="checkbox" value="true" class="require-one" />
<input name="checkbox3" type="hidden" value="false" /><br />

<input type="submit" />
</form>

来自:https://stackoverflow.com/a/11512622/1026898