title: MVC 3 Unobtrusive validation of a list
body: <p><strong>Question</strong></p>
  
  <p>I have created a server-side property level validation attribute. But instead of applying it to an individual field I've applied it to a List. This allows me to validate the model as a whole. </p>
  
  <p>I now need to know how to convert this to work using the unobtrusive client-side validation built into MVC 3. </p>
  
  <p>My current code is below to illustrate my issue...</p>
  
  <p><strong>Scenario</strong></p>
  
  <p>The basic scenario was the ability total up all the Quantity values for every row in a List grouped by the GroupNo field.  If the sum of any of the groups was more than 10 then an error should be displayed.</p>
  
  <p>I was kindly given an answer in a previous post to make this work server-side using a validation attribute against a List...</p>
  
  <p>The model:</p>
  
  <pre><code>public class ItemDetails
  {
      public int SerialNo { get; set; }
      public string Description { get; set; }
      public int GroupNo { get; set; }
      public decimal Price { get; set; }
      public int Quantity { get; set; }
  }
  
  public class MyViewModel
  {
      [EnsureMaxGroupItems(10, ErrorMessage = "You cannot have more than 10 items in each group")]
      public IList&lt;ItemDetails&gt; Items { get; set; }
  }
  </code></pre>
  
  <p>and the validation attribute itself:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Property)]
  public class EnsureMaxGroupItemsAttribute : ValidationAttribute
  {
      public int MaxItems { get; private set; }
  
      public EnsureMaxGroupItemsAttribute(int maxItems)
      {
          MaxItems = maxItems;
      }
  
      public override bool IsValid(object value)
      {
          var items = value as IEnumerable&lt;ItemDetails&gt;;
          if (items == null)
          {
              return true;
          }
  
          return items
              .GroupBy(x =&gt; x.GroupNo)
              .Select(g =&gt; g.Sum(x =&gt; x.Quantity))
              .All(quantity =&gt; quantity &lt;= MaxItems);
      }
  }
  </code></pre>
  
  <p>and finally your controller actions will work with the view model:</p>
  
  <pre><code>public ActionResult ListItems()
  {
      var model = new MyViewModel
      {
          Items = ItemsRepository.GetItems()
      };
      return View(model);
  }
  
  [HttpPost]
  public ActionResult ListItems(MyViewModel model)
  {
      if (!ModelState.IsValid)
      {
          return View(model);
      }
  
      ...
  }
  </code></pre>
  
  <p>and next the corresponding strongly typed view:</p>
  
  <pre><code>@model MyViewModel
  @Html.ValidationSummary()
  @using (Html.BeginForm())
  {
      @Html.EditorFor(x =&gt; x.Items)
      &lt;button type="submit"&gt;Go go go&lt;/button&gt;
  }
  </code></pre>
  
  <p>and the last bit is the corresponding editor template that will automatically be rendered for each element of the Items collection so that you don't even need to write for loops (<code>~/Views/Shared/EditorTemplates/ItemDetails.cshtml</code>):</p>
  
  <pre><code>@model ItemDetails
  @Html.HiddenFor(x =&gt; x.SerialNo)
  @Html.LabelFor(x =&gt; x.Description)
  @Html.HiddenFor(x =&gt; x.GroupNo)
  @Html.LabelFor(x =&gt; x.Price)
  @Html.TextBoxFor(x =&gt; x.Quantity)
  </code></pre>
  
  <p><strong>Client-side unobtrusive validation possible?</strong></p>
  
  <p>I would like it all to validate using unobtrusive MVC validation.  But I cannot figure out how to unobtrusively validate the EnsureMaxGroupItemsAttribute attribute against the list as a whole.</p>
  
  <p>I've implemented IClientValidatable in this way:</p>
  
  <pre><code>    Public Function GetClientValidationRules(metadata As System.Web.Mvc.ModelMetadata, context As System.Web.Mvc.ControllerContext) As System.Collections.Generic.IEnumerable(Of System.Web.Mvc.ModelClientValidationRule) Implements System.Web.Mvc.IClientValidatable.GetClientValidationRules
  
          Dim result = New List(Of ModelClientValidationRule)
  
          Dim rule = New ModelClientValidationRule() With { _
              .ErrorMessage = "You cannot have more than 10 items in each group", _
              .ValidationType = "itemscheck"}
  
          result.Add(rule)
  
          Return result
  
      End Function
  </code></pre>
  
  <p>Note: the mix of VB and C# is only because the previous question I asked was answered in C#. The project is in VB but I don't mind an answer in C#. </p>
  
  <p>I've created the adaptor in my JS file:</p>
  
  <pre><code>jQuery.validator.unobtrusive.adapters.addBool("itemscheck"); 
  </code></pre>
  
  <p>... and ...</p>
  
  <pre><code>jQuery.validator.addMethod("itemscheck", function (value, element, params) {
      // The check has been omitted for the sake of saving space.  
      // However this method never gets called
      return false;
  });
  </code></pre>
  
  <p>Is there a way to hook this up to work unobtrusively?</p>