title: MVC 3 Razor @Html.ValidationMessageFor not working in partial loaded via jquery.load()
body: <p>I have put together a small example here just to replicate the problem.
  I have a strongly typed partial view _Name.cshtml:</p>
  
  <pre><code>@model ValidationInPartial.ViewModels.MyViewModel
  
  &lt;h2&gt;@ViewBag.Message&lt;/h2&gt;
  
      &lt;fieldset&gt;
          &lt;legend&gt;Name&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.MyName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.MyName)
              @Html.ValidationMessageFor(model =&gt; model.MyName)
          &lt;/div&gt;
  
          &lt;a href="#" id="reload"&gt;Reload Name&lt;/a&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  
  &lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          $("#reload").click(function () {
              $("#divName").load("Home/NameReload");
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>that is initially loaded and displayed inside the main Index.cshtml</p>
  
  <pre><code>&lt;div id="divForm"&gt;
      @using (Html.BeginForm()) {
  
          &lt;div id="divName"&gt;
              @Html.Partial("_Name")
          &lt;/div&gt;
      }
  &lt;/div&gt;
  </code></pre>
  
  <p>The field MyName is required and validation is implemented through Required attribute in MyViewModel</p>
  
  <pre><code>namespace ValidationInPartial.ViewModels
  {
      public class MyViewModel
      {
          [Required(ErrorMessage = "Please enter a Name.")]
          public string MyName { get; set; }
      }
  }
  </code></pre>
  
  <p>After the page is loaded the first time, if you click the Create button leaving the field empty the validation message "Please enter a Name." shows beside the field and the field itself turns pink, which is the expected behaviour.
  Now by clicking the "Reload Name" link, which makes an ajax call (jquery.load(...)), the partial is reloaded, here is controller code:</p>
  
  <pre><code>public PartialViewResult NameReload()
  {
      MyViewModel myViewModel = new MyViewModel();
      ViewBag.Message = "Name Reloaded";
      return PartialView("_Name", myViewModel);
  }
  </code></pre>
  
  <p>This time if you click the Create button leaving the field empty the validation message does not appear beside the field, although the field turns pink.
  It turns out that when reloading the partial the @Html.ValidationMessageFor doesn't render the validation message as the first time.</p>
  
  <p>Here is the jquery files I use</p>
  
  <pre><code>&lt;script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>I wonder if this is a bug in the way the Razor engine renders the @Html.ValidationMessageFor or is that a problem with jquery?
  Any idea why this happens?</p>
  
  <p>I have also read somewhere that the ajax call looses all the scripts for the page, in fact I have to keep any javascript code inside the partial so that they can be rendered and used again.</p>
  
  <p>In the meantime I found a workaround which is to manually render in the partial what was supposed to be rendered by @Html.ValidationMessageFor which is:</p>
  
  <pre><code>&lt;span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="MyName"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>However this workaround means that if we change the type of validation or just the validation message inside the Required attribute in the ViewModel, we need to modify this hard-coded piece of html in the view.</p>