收集的不显眼的审定

编程入门 行业动态 更新时间:2024-10-27 18:18:59
本文介绍了收集的不显眼的审定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的模型包含集合:

public ICollection<int> ChildAges { get; set; }

这是可以添加到年龄的动态列表,这是通过JQuery的所有控制。

This is a dynamic list of ages that can be added to, this is all controlled via JQuery.

给我

<select name="ChildAges">...</select> <select name="ChildAges">...</select> <select name="ChildAges">...</select> etc...

如果我添加标准要求属性,如果集合中的任何一个值设置验证返回true。

If I add the standard Required attribute the validation returns true if any one value in the collection is set.

我如何可以验证所有的 ChildAges 的形式设置?

How can I validate that all ChildAges in the form are set?

推荐答案

我创建了一个新的自定义 IClientValidatable 属性:

I created a new custom IClientValidatable attribute:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class MultipleRequiredValuesAttribute : RequiredAttribute, IClientValidatable { #region IClientValidatable Members public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var clientValidationRule = new ModelClientValidationRule() { ErrorMessage = base.ErrorMessage, ValidationType = "multiplerequiredvalues" }; return new[] { clientValidationRule }; } #endregion }

和应用这对我的模型:

[DisplayName("Ages(s)")] [MultipleRequiredValues(ErrorMessage = "You must provide ages for all children in all rooms")] public ICollection<int> ChildAges { get; set; }

我可以再添加JQuery的一面:

I can then add the JQuery side:

(function ($) { $.validator.addMethod('multiplerequiredvalues', function (value, element) { if ($(element).is(':visible')) { var returnVal = true; var name = $(element).attr('name'); var elements; if ($(element).is('input')) { elements= $('input[name='+name+']'); } else { elements= $('select[name='+name+']'); } elements.each(function() { if ($(this).is(':visible')) { returnVal = $(this).val() != "" && $(this).val() != null; } }); return returnVal; } else { return true; } }); $.validator.unobtrusive.adapters.addBool("multiplerequiredvalues"); } (jQuery));

注意如果该元素是不可见的,这也将返回true

Note this also returns true if the element isn't visible

更多推荐

收集的不显眼的审定

本文发布于:2023-06-04 22:10:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/506090.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:显眼

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!