title: MVC3 Unobtrusive Validation Not Working after Ajax Call
body: <p>Ok, here is the deal, I have seen a few posts on SO relating to this issue, but nothing is working for me.   </p>
  
  <p>Basically, I have select drop downs that are being loaded from partial views, I am trying to filter contents of each subsequent drop down, based on the previously selected drop down.</p>
  
  <p>If I just put the call to the partial view in the div containers, and load the page, the validation from data annotations works fine, <em>primarily Required attribute</em>.</p>
  
  <p>However, if I try to load the same partial via AJAX as it is setup here, the Required validation does not work, anyone can post the form after that and KABOOM.</p>
  
  <p>I have found people saying that in the Success callback you need to have the client side validator reparse the form, and I am trying that, but it doesn't seem to be working.</p>
  
  <p>I have a view which looks like this...</p>
  
  <pre><code>  @model Area51.Models.Workflow.AddReportableItemToBatchActionModel
  @{
      ViewBag.Title = "Add Reportable Item to Batch";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;script type="text/javascript"&gt;
  
      $(function () {
          var fadeDelay = 150;
  
          $(".jqDatePicker").datepicker({
              dateFormat: 'm/d/yy',
              onSelect: function (date) {
                  $("#categoryContainer").show(fadeDelay);
              }
          });
  
          $('#Category').change(function () {
              RetrieveItemsForCategory();
              $("#itemContainer").show(100);
          });
  
          $('#Item').live('change', function () {
              RenderPartialForUOMByItem();           
          });
  
  
  
          function RetrieveItemsForCategory() {
  
              var category = $("#Category :selected").val();
  
              $.ajax({
                  type: "POST",
  
                  url: '@Url.Action("RenderPartialForLocationItemsByCategory","BatchWorkflow")',
  
                  data: 'category=' + category,
  
                  success: function (result) {
                      $("#itemContainer").html(result.toString());
                      $("#itemContainer").show(100);
                      RebindValidation();
                  },
  
                  error: function (req, status, error) {
                      alert("Sorry! Could not request items for your selection at this time.");
                  }
  
              });
  
  
          }
  
  
          function RenderPartialForUOMByItem() {
  
              var item = $("#Item :selected").val();
  
              $.ajax({
                  type: "POST",
  
                  url: '@Url.Action("RenderPartialForUOMByItem","BatchWorkflow")',
  
                  data: "item=" + item,
  
                  success: function (result) {
                      $("#quantityContainer").html(result.toString());
                      $("#quantityContainer").show(100);
                      RebindValidation();
                  },
  
                  error: function (req, status, error) {
                      alert("Sorry! Could not request items for your selection at this time.");
                  }
  
              });
          }
  
          function RebindValidation() {
              alert("Rebinding Validation");
              $.validator.unobtrusive.parse("#frmAddItem");
          }
  
      });      // End OnLoad Event
  &lt;/script&gt;
  
  &lt;h3 class="pageHeader"&gt;Batch : @Model.BatchName&lt;/h3&gt;
  
  &lt;div align="center"&gt;
  
  @{Html.BeginForm("AddItemToBatch", "BatchWorkflow", null, FormMethod.Post, new { id = "frmAddItem" });}
  
      @Html.ValidationSummary(true)
  
      &lt;fieldset style="width:60%"&gt;
          &lt;legend&gt;Add an Item to the Batch&lt;/legend&gt;     
  
       &lt;div&gt;       
            &lt;h3&gt;Select Date Item was Added&lt;/h3&gt;
            @Html.EditorFor(x =&gt; x.EventDate,null)
            &lt;br /&gt;
        &lt;/div&gt;
  
        &lt;div id="categoryContainer" style="display:none"&gt; 
          &lt;hr /&gt;
            &lt;h3&gt;Select an Inventory Category&lt;/h3&gt;
            @Html.EditorFor(x =&gt; x.Category,null)
            &lt;br /&gt;
        &lt;/div&gt;
  
        &lt;div id="itemContainer" style="display:none"&gt; 
          @*   @{Html.RenderAction("RenderPartialForLocationItemsByCategory", "BatchWorkflow", new { category = Model.Category });}*@
        &lt;/div&gt;
  
  
        &lt;div id="quantityContainer" style="display:none"&gt; 
          @*  @{Html.RenderAction("RenderPartialForUOMByItem", "BatchWorkflow", new { item = Model.Item });}*@
        &lt;/div&gt;
  
        &lt;div id="reportingDataContainer" style="display:none"&gt; 
          &lt;hr /&gt;
            &lt;h3&gt;What quantity of the batch was affected by this addition?&lt;/h3&gt;
            @Html.EditorFor(x =&gt; x.ConsumedWineQuantity) (Gallons)
          &lt;br /&gt;
          &lt;hr /&gt;
            &lt;h3&gt;What was the increase in Batch Volume as a result of this addition?&lt;/h3&gt;
            @Html.EditorFor(x =&gt; x.ProducedWineQuantity) (Gallons)
        &lt;/div&gt;
  
          &lt;div style="display:block"&gt;
          &lt;div&gt;&lt;/div&gt;        
              &lt;span&gt;&lt;button type="button" id="btnCancel" class="linkButton" value="Cancel" onclick="location.href='@Url.Action("Home","Home",null)';"&gt;Cancel&lt;/button&gt;&lt;/span&gt;  
              &lt;span&gt;&lt;button type="submit" id="btnSubmit" class="linkButton" value="Add"&gt;Add Item&lt;/button&gt;&lt;/span&gt;
          &lt;/div&gt;
  
  
      &lt;/fieldset&gt;
          @{ Html.EndForm(); }
  &lt;/div&gt;
  </code></pre>
  
  <p>The Partial Views are very simple, they basically look like this...</p>
  
  <pre><code>@model Area51.Models.Workflow.AddReportableItemToBatchActionModel
  
        &lt;hr /&gt;
            &lt;h3&gt;Select the Item to Add&lt;/h3&gt;
            @Html.EditorFor(x =&gt; x.Item)
            &lt;br /&gt;
  </code></pre>
  
  <p>Again, if I just RenderPartial, the validation works fine, however when I try to do it via ajax, the validation goes away.   The "Rebinding Validation" alert fires, but the $.validator.unobtrusive.parse("#frmAddItem"); doesn't seem to be doing a thing.</p>
  
  <p>Can anyone help with what I am missing?  It would be greatly appreciated.</p>
  
  <p>&lt;=======================    UPDATE 1   =============================></p>
  
  <p>OK, I tried adding the $.validator.unobtrusive.parse("#frmAddItem"); at the bottom of the the partial view in a document ready event and it didn't seem to work either, basically nothing changed, I could still submit the form. </p>
  
  <p>I did find a post here : <a href="http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/">http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/</a> that mentioned that when the MVC version of the jqvalidation sees a form already has validation rules bound to it, it just ignores the .validator call.  I implemented the script extension that this gentleman used, and the validation is now rebinding to the form using the new extension. I can test this by appending html to the form and calling the new extension, and it is rebinding to the new text box. </p>
  
  <p>However, this still has not completely fixed the issue.   I used Firebug to check out the actual content on the fields coming back from the ajax call, and noticed something very strange.   </p>
  
  <p>When I use the RenderPartial to call the action, it writes out the following select :</p>
  
  <pre><code>&lt;select id="Item" name="Item" data-val-required="The Item field is required." data-val-number="The field Item must be a number." data-val="true"&gt;
  </code></pre>
  
  <p>However, when I make the ajax call to the same exact controller action, it gives me this back :</p>
  
  <pre><code>&lt;select id="Item" name="Item"&gt;
  </code></pre>
  
  <p>I tried adding the script tags to the partial view as well, but it didn't fix the issue.  Is there some reason why the ajax call would be stripping the unobtrusive validation tags?</p>
  
  <p>&lt;=======================    UPDATE 2   =============================></p>
  
  <p>Ok, so what was happening, is I had an editor template for the drop down that took a select list and converted it to an html select.   I found a post that mentioned that in order to get data validation attributes to write out on an editor template, you have to have a form context.   Since the Html.RenderPartial was being done within a form, then the editor template had a form context to work with.  When I was just trying to call the partial via ajax, there was no form context to work with, and instead of complaining it just didn't write out the data validation attributes.  Adding a new Form Context in the editor template for the SelectListDropDown fixed the issue. </p>
  
  <pre><code>@{ // fix to stop stupid crappy brad wilson mvc3 code from stripping the jq data valdiation attributes
      if (ViewContext.FormContext == null)
      {
          ViewContext.FormContext = new FormContext();
      }
  }
  </code></pre>