title: MVC Data Annotations in Razor
body: <p>I'm trying to use Data Annotations in my MVC project (Mono/.NET 4.5). I've created my model and added all the appropriate annotations.  I have my view and controller appropriately wired up.  However, the validation just doesn't seem to be happening.  I've tried everything I can find to no avail.  As this is my first time working with Razor and Data Annotations, I imagine there is some setup piece I'm missing but I can't find it for the life of me.  Here's my code:</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  using System.Web.Mvc;
  
  namespace MyWebsite
  {
    public class RegisterViewModel : BaseViewModel
    {
      #region properties
      [Required(ErrorMessage = "Required")]
      [StringLength(50)]
      [DisplayName("First Name")]
      public string FirstName { get; set; }
      [Required(ErrorMessage = "Required")]
      [StringLength(100)]
      [DisplayName("Last Name")]
      public string LastName { get; set; }
      [StringLength(50)]
      [DisplayName("Display Name")]
      public string DisplayName { get; set; }
      [Required(ErrorMessage = "Required")]
      [StringLength(255)]
      [DisplayName("Email Address")]
      public string EmailAddress { get; set; }
      #endregion
  
      #region ctor
      public RegisterViewModel ()
      {
  
      }
      #endregion
  
    }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace MyWebsite.Controllers
  {
    public class AccountController : Controller
    {
      public ActionResult Register()
      {
          ViewData ["IsComplete"] = false;
          ViewData ["RequiredVouches"] = WebSettings.RequiredVouches;
  
          return View ();
      }
  
      [HttpPost]
      public ActionResult Register(FormCollection formData)
      {
          if (ModelState.IsValid) {
              //TODO: Put the data
              ViewData ["IsComplete"] = true;
          }
          return View ();
      }
    }
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model SummerIsles.Web.RegisterViewModel
  @section Styles {
  &lt;link href="~/Content/themes/base/Account.css" rel="stylesheet" type="text/css" /&gt;
  }
  @{
  if((bool)@ViewData["IsComplete"]) {
      &lt;h1&gt;Registration Request Complete!&lt;/h1&gt;
      &lt;div class="page-message"&gt;
          &lt;p&gt;
             Confirmation message goes here
          &lt;/p&gt;
      &lt;/div&gt;
  }
  else {
      &lt;h1&gt;Registration&lt;/h1&gt;
      &lt;div class="page-message"&gt;
          &lt;p&gt;
              Instruction text goes here
          &lt;/p&gt;
      &lt;/div&gt;
      using (Html.BeginForm()) {
          @Html.ValidationSummary(false)
          &lt;fieldset&gt;
              &lt;legend&gt;Your Information&lt;/legend&gt;
  
              &lt;div class="group column-1"&gt;
                  @Html.LabelFor(modelItem =&gt; @Model.FirstName)
                  @Html.EditorFor(modelItem =&gt; @Model.FirstName, new { htmlAttributes = new { @class="form-control" } } )
                  @Html.ValidationMessageFor(modelItem =&gt; @Model.FirstName)
  
                  @Html.LabelFor(modelItem =&gt; @Model.DisplayName)
                  @Html.EditorFor(modelItem =&gt; @Model.DisplayName, new { htmlAttributes = new { @class="form-control" } } )
                  @Html.ValidationMessageFor(modelItem =&gt; @Model.DisplayName)
              &lt;/div&gt;
  
              &lt;div class="group column-2"&gt;
                  @Html.LabelFor(modelItem =&gt; @Model.LastName)
                  @Html.EditorFor(modelItem =&gt; @Model.LastName, new { htmlAttributes = new { @class="form-control" } } )
                  @Html.ValidationMessageFor(modelItem =&gt; @Model.LastName)
  
                  @Html.LabelFor(modelItem =&gt; @Model.EmailAddress)
                  @Html.EditorFor(modelItem =&gt; @Model.EmailAddress, new { htmlAttributes = new { @class="form-control" } } )
                  @Html.ValidationMessageFor(modelItem =&gt; @Model.EmailAddress)
              &lt;/div&gt;
  
              &lt;div class="button-options"&gt;
                  &lt;button id="btnSubmit" type="submit" formmethod="post" class="btn btn-danger"&gt;Submit&lt;/button&gt;
                  &lt;a id="btnCancel" href="~/" class="btn"&gt;Cancel&lt;/a&gt;
              &lt;/div&gt;
          &lt;/fieldset&gt;
      }
    }
  }
  </code></pre>
  
  <p>Also, I have added the jquery validation scripts to my layout file and have also enabled client validation in the web.confg.</p>
  
  <p><strong>Layout Header</strong></p>
  
  <pre><code>&lt;head&gt;
    &lt;!--some other css and such--&gt;
    &lt;script src="~/Scripts/jquery.validate.min.js"&gt;&lt;/script&gt;
    &lt;script src="~/Scripts/jquery.validate.unobstrusive.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  </code></pre>
  
  <p><strong>Web.config</strong></p>
  
  <pre><code>&lt;appSettings&gt;
     &lt;!--some other stuff--&gt;
     &lt;add key="ClientValidationEnabled" value="true" /&gt;
     &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
  &lt;/appSettings&gt;
  </code></pre>
  
  <p>Basically, when I click submit, it thinks the model is perfectly valid (ModelState.IsValid returns true in the controller) and the validation stuff (which I would expect to fire before the post back to the controller) never seems to fire.  I get no validation messages even with a completely blank form (despite having the "Required" data annotation).  What am I missing?</p>