title: MVC/ASP.NET: Client side validation not working
body: <p>EDIT: Consider this whole question replaced by this one: 
  <a href="https://stackoverflow.com/questions/44458268/asp-net-mvc-knockout-binding-preventing-client-side-validation-from-being-perfo">ASP.NET/MVC: Knockout-binding preventing client-side validation from being performed?</a></p>
  
  <p>Working in ASP.NET/MVC, I’m having great trouble getting the (unobtrusive or even non-unobtrusive) client side validation to work. (Yes again, but this time for different reasons as it appears.)</p>
  
  <p>NB: The EDITS towards the bottom of the post are important, esp. EDITS 2 and 3.</p>
  
  <p>Problem is, client side validation does absolutely nothing (see below), even though all ingredients seem to be in place. I’ve read through quite a few questions and answers here on Stack Overflow, and it <em>seems</em> everything is done correctly in my project, but still it doesn’t work. What am I missing?</p>
  
  <p>In my Web.config file (the one in the root folder, not the one in the View folder), I’ve explicitly enabled the client side validation:</p>
  
  <pre><code>&lt;appSettings&gt;
      &lt;add key="webpages:Version" value="3.0.0.0" /&gt;
      &lt;add key="webpages:Enabled" value="false" /&gt;
      &lt;add key="ClientValidationEnabled" value="true" /&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
  &lt;/appSettings&gt;
  </code></pre>
  
  <p>And all the relevant Javascript files are there, in the right order and loading correctly (and I’ve made no changes to them obviously). I’ve included them in bundles, in BundleConfig:</p>
  
  <pre><code>bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                          "~/Scripts/jquery-{version}.js"));
  bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                          "~/Scripts/jquery.validate.js", "~/Scripts/jquery.validate.unobtrusive.js"));
  </code></pre>
  
  <p>I add these bundles in my View (well actually in the shared _Layout.cshtml parent view):</p>
  
  <pre><code>    @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/jqueryval")
      @Scripts.Render("~/bundles/bootstrap")
      @Scripts.Render("~/bundles/knockout")
  </code></pre>
  
  <p>And in the generated source code for the View these scripts (and a few others) appear as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/gvqry.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gvqry.png" alt="enter image description here"></a></p>
  
  <p>Which, as far as I can tell, is correct. (I also use the jQuery as well as knockout extensively in other places in the same View.) Also, no loading errors in the browser console.</p>
  
  <p>Then in my ViewModel, I have this annotated property Email (among others):</p>
  
  <pre><code>public class RegisterViewModel
  
       {
              [Required]
              [EmailAddress]
              [Display(Name = "Email")]
              public string Email { get; set; }
  
              // some other properties…
        }
  </code></pre>
  
  <p>And this is the corresponding code in my View:</p>
  
  <pre><code>@Html.ValidationSummary("", new { @class = "text-danger" })
      &lt;div class="form-group"&gt;
          @Html.LabelFor(m =&gt; m.Email, new { @class = "col-md-2 control-label" })
          &lt;div class="col-md-10"&gt;
              @Html.TextBoxFor(m =&gt; m.Email, new { @class = "form-control" })
              @Html.ValidationMessageFor(m =&gt; m.Email)
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Which generates the following source code (click to enlarge):</p>
  
  <p><a href="https://i.stack.imgur.com/EYv1i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EYv1i.png" alt="Which also seems to be correct as fa"></a></p>
  
  <p>Which also seems correct, as far as I can tell.</p>
  
  <p>But then when I type something that’s evidently not a valid email-address in the input field, and make the field lose focus (by jumping to the next field), NO ERROR MESSAGE appears. Neither in the summary, nor the specific error message for the email field itself. As seen here:</p>
  
  <p><a href="https://i.stack.imgur.com/Gkdd6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gkdd6.png" alt="enter image description here"></a></p>
  
  <p>(The little red square is the .field-validation-valid element that should contain the error message, but doesn't. For testing purposes I had decorated this element with a red border.)</p>
  
  <p>Also, when I press the submit button, the input fields are simply submitted to the server action method (which then correctly determines that the inputs are not valid). But in fact, with client side validation enabled, the submit shouldn’t happen unless client side validation succeeds. But well, that’s basically the whole issue: client side validation simply doesn’t happen, despite the correct HTML being generated (presumably!).</p>
  
  <p>EDIT 1: </p>
  
  <p>And here is the HTML/Razor for the whole form:</p>
  
  <pre><code>@model Treinen2.Models.RegisterViewModel
  @{
      ViewBag.Title = "Register";
  }
  
  @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  {
      @Html.AntiForgeryToken()
      &lt;h4&gt;Create a new account.&lt;/h4&gt;
      &lt;hr /&gt;
      @Html.ValidationSummary("", new { @class = "text-danger" })
      &lt;div class="form-group"&gt;
          @Html.LabelFor(m =&gt; m.Email, new { @class = "col-md-2 control-label" })
          &lt;div class="col-md-10"&gt;
              @Html.TextBoxFor(m =&gt; m.Email, new { @class = "form-control" })
              @Html.ValidationMessageFor(m =&gt; m.Email)
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          @Html.LabelFor(m =&gt; m.Password, new { @class = "col-md-2 control-label" })
          &lt;div class="col-md-10"&gt;
              @Html.PasswordFor(m =&gt; m.Password, new { @class = "form-control" })
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          @Html.LabelFor(m =&gt; m.ConfirmPassword, new { @class = "col-md-2 control-label" })
          &lt;div class="col-md-10"&gt;
              @Html.PasswordFor(m =&gt; m.ConfirmPassword, new { @class = "form-control" })
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input type="submit" class="btn btn-default" value="Register" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>This is in a shared View called Register.cshtml, which -- depending on certain conditions -- gets displayed by calling @Html.Partial("Register") from the home view Index.cshtml. [I'm kind of trying to build a single page API application, as an exercise, so everything is included in the homepage (Index), and all navigation happens client side (with the obvious exception of the submit of the "Register" button itself, which gets sent to the server Action method, at least for now). And with obviously only one part ('page') of Index.cshtml being displayed at any one time, for which I use knockout binding.]</p>
  
  <p>EDIT 2:</p>
  
  <p>OK, I've noticed the following: If I put the same form in Index.cshtml, and also add the same model to that Index view, then client-side validation works perfectly. It's only on the partial view that it fails to work. Which suggests that I could simply stop using the partial view, and instead simply include its content on Index.cshtml, as tested. On the other hand, I still don't understand WHY using the partial view for the register form wouldn't work too. (With this new information, I may have to ask the question anew in a different form, but don't have the time right now...) [/EDIT 2]</p>
  
  <p>EDIT 3:</p>
  
  <p>Seems I'm edging closer to understanding the root cause of the problem. Because now I have the validation working with the form on the Index view... BUT it stops working as soon as I put the whole form inside a DIV-element that is bound by a Knockout attribute data-bind="If: ....." So that seems to be the essence of the problem: the interaction between client-side validation and Knockout binding. The Knockout binding, which in and of itself works fine, seems to prevent the validation from being performed. Although I still don't understand why that should be the case. [/EDIT 3]</p>
  
  <p>So, what am I missing? Any constructive suggestions appreciated, will check back in a few hours.</p>