Note that there are some explanatory texts on larger screens.

plurals
  1. POcheck collection length - unobtrusive validation - asp.net mvc 3, 4
    primarykey
    data
    text
    <p>I've got <a href="https://github.com/ypapax/MyCollectionCountValidatorClient" rel="nofollow">custom ValidationAttribute with implemented IClientValidatable</a>. It works great using(Html.BeginForm()) but not working using(Ajax.BeginForm()).</p> <p>Client code:</p> <pre><code> $(function() { jQuery.validator.unobtrusive.adapters.addBool('collectionLength'); }); </code></pre> <p>Server code:</p> <pre><code>/// &lt;summary&gt; /// Require a minimum length, and optionally a maximum length, for any IEnumerable /// &lt;/summary&gt; sealed public class CollectionMinimumLengthValidationAttribute : ValidationAttribute, IClientValidatable { const string errorMessage = "{0} must contain at least {1} item(s)."; const string errorMessageWithMax = "{0} must contain between {1} and {2} item(s)."; int minLength; int? maxLength; public CollectionMinimumLengthValidationAttribute(int min) { minLength = min; maxLength = null; } public CollectionMinimumLengthValidationAttribute(int min, int max) { minLength = min; maxLength = max; } //Override default FormatErrorMessage Method public override string FormatErrorMessage(string name) { if (maxLength != null) { return string.Format(errorMessageWithMax, name, minLength, maxLength.Value); } else { return string.Format(errorMessage, name, minLength); } } public override bool IsValid(object value) { IEnumerable&lt;object&gt; list = value as IEnumerable&lt;object&gt;; if (list != null &amp;&amp; list.Count() &gt;= minLength &amp;&amp; (maxLength == null || list.Count() &lt;= maxLength)) { return true; } else { return false; } } public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ValidationType = " CollectionMinimumLengthValidation", ErrorMessage = FormatErrorMessage("Error") }; rule.ValidationParameters.Add("min", minLength); rule.ValidationParameters.Add("max", maxLength); yield return rule; } } </code></pre> <p>CollectionMinimumLengthValidation.js file:</p> <pre><code>jQuery.validator.addMethod("CollectionMinimumLengthValidation", function (value, element, param) { debugger; return false; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload