title: ASP.NET MVC 4 - Clientside Validation Not Working
body: <p>I am using Visual Studio 2012 and I cannot get a custom attribute client side logic to work
  to reproduce at a smaller scale, I created a new MVC 4 project I created the following model and Attribute that will never validate</p>
  
  <pre><code>public class MyModel
  {
      public int Id { get; set; }
      [Required]
      public string LastName { get; set; }
      [NeverValid(ErrorMessage="Serverside Will Never Validate")]
      public string FirstName { get; set; }
  }
  
  public class NeverValidAttribute : ValidationAttribute, IClientValidatable
  {
      public override bool IsValid(object value)
      {
          return false;
      }
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          return new ValidationResult(this.ErrorMessage, new[] { validationContext.MemberName });
      }
  
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          yield return new ModelClientValidationRule
          {
              ErrorMessage = this.ErrorMessage,
              ValidationType = "nevervalid"
          };
      }
  }
  </code></pre>
  
  <p>I then have the following actions added to the HomeController</p>
  
  <pre><code>public ActionResult Index()
  {
      return View(new MyModel());
  }
  
  [HttpPost]
  public ActionResult Index(MyModel model)
  {
      if (!ModelState.IsValid)
      {
          // Will Always Be Invalid
      }
  
      return View(model);
  }
  </code></pre>
  
  <p>There is also a javascript file called nevervalid.js</p>
  
  <pre><code>$(function () {
      $.validator.addMethod("nevervalid", function () {
          return false;
      }, "Clientside Should Not Postback");
  
      $.validator.unobtrusive.adapters.addBool("nevervalid");
  });
  </code></pre>
  
  <p>and the Index View</p>
  
  <pre><code>@model CustomAttribute.Models.MyModel
  
  @{
      ViewBag.Title = "Home Page";
  }
  
  @using (Html.BeginForm())
  {
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;MyModel&lt;/legend&gt;
  
          @Html.HiddenFor(model =&gt; model.Id)
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.LastName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.LastName)
              @Html.ValidationMessageFor(model =&gt; model.LastName)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.FirstName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.FirstName)
              @Html.ValidationMessageFor(model =&gt; model.FirstName)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
      @Scripts.Render("~/Scripts/nevervalid.js")
  }
  </code></pre>
  
  <p>The relevant areas in my web.config look like this</p>
  
  <pre><code>&lt;appSettings&gt;
      &lt;add key="webpages:Version" value="2.0.0.0" /&gt;
      &lt;add key="webpages:Enabled" value="false" /&gt;
      &lt;add key="PreserveLoginUrl" value="true" /&gt;
      &lt;add key="ClientValidationEnabled" value="true" /&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
  &lt;/appSettings&gt;
  </code></pre>
  
  <p>when the page loads, the following files are loaded (got this from network tab under chrome's F12)</p>
  
  <pre><code>http://localhost:7440/
  http://localhost:7440/Content/site.css
  http://localhost:7440/Scripts/modernizr-2.5.3.js
  http://localhost:7440/Scripts/jquery-1.7.1.js
  http://localhost:7440/Scripts/jquery.unobtrusive-ajax.js
  http://localhost:7440/Scripts/jquery.validate.js
  http://localhost:7440/Scripts/jquery.validate.unobtrusive.js
  http://localhost:7440/Scripts/nevervalid.js
  </code></pre>
  
  <p>and my custom attribute adds relevant looking data- stuff to the first name input like so...</p>
  
  <pre><code>    &lt;input class="text-box single-line valid" data-val="true" data-val-nevervalid="Serverside Will Never Validate" id="FirstName" name="FirstName" type="text" value=""&gt;
  </code></pre>
  
  <p>so, I ask you, why oh why does this thing have to postback to do serverside validation while I have some perfectly looking javascript code here? do I have to sacrifice some animal on a moonless night on top of a hill somewhere?</p>