title: REQUIRED and RANGE validation in ASP.NET MVC not working
body: <p>I just started learning ASP.NET MVC (<em>NOT core</em>), and can't seem to validate models. A snippet of my code is as follows:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Web;
  
  namespace MVCDatabasePractice.Models
  {
      public class EmployeeModel
      {
          [Display(Name = "Employee ID")]
          [Range(100000, 999999)]
          public int EmployeeId { get; set; }
  
          [Display(Name = "First Name")]
          [Required]
          public string FirstName { get; set; }
  
          [Display(Name = "Last Name")]
          [Required(ErrorMessage = "Enter in last name")]
          public string LastName { get; set; }
  
          [Display(Name = "Email Address")]
          [Required(ErrorMessage = "Enter in email address")]
          [DataType(DataType.EmailAddress)]
          public string EmailAddress { get; set; }
  
          [Display(Name = "Confirm Email")]
          [Compare("EmailAddress", ErrorMessage = "Confirm email doesn't match email address")]
          public string ConfirmEmail { get; set; }
  
          [Display(Name = "Password")]
          [Required(ErrorMessage = "Enter in password")]
          [DataType(DataType.Password)]
          [StringLength(100, MinimumLength = 8, ErrorMessage = "Password length incorrect")]
          public string Password { get; set; }
  
          [Display(Name = "Confirm Password")]
          [DataType(DataType.Password)]
          [Compare("Password", ErrorMessage = "Confirm password doesn't match password")]
          public string ConfirmPassword { get; set; }
  
      }
  }
  </code></pre>
  
  <p>The <code>[Display(Name = "...")]</code> part works perfectly fine in the final form, but the <code>[Range]</code>, <code>[Compare]</code> and <code>[Required]</code> just don't work. (<code>[DataType(DataType.Password)]</code> still works, though).</p>
  
  <p>Passing them more parameters like <code>ErrorMessage = "This field is required"</code> doesn't work either.</p>
  
  <p>What am I doing wrong? I'm not getting any errors whatsoever.</p>
  
  <p>For those asking, this is the code in the controller, extremely basic:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace MVCDatabasePractice.Controllers
  {
      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              return View();
          }
  
          public ActionResult About()
          {
              ViewBag.Message = "Your application description page.";
  
              return View();
          }
  
          public ActionResult Contact()
          {
              ViewBag.Message = "Your contact page.";
  
              return View();
          }
  
          public ActionResult SignUp()
          {
              ViewBag.Message = "Employee Sign Up";
  
              return View();
          }
      }
  }
  </code></pre>
  
  <p>And the code in the view file:</p>
  
  <pre><code>@model MVCDatabasePractice.Models.EmployeeModel
  
  @{
      ViewBag.Title = "Employee Sign Up";
  }
  
  &lt;h2&gt;SignUp&lt;/h2&gt;
  
  @using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Employee Model&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.EmployeeId, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.EmployeeId, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.EmployeeId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.FirstName, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.LastName, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.EmailAddress, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.ConfirmEmail, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.ConfirmEmail, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.ConfirmEmail, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Password, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Password, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.ConfirmPassword, "", new { @class = "text-danger" })
              &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" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>