title: ASP.NET MVC: ModelState error message not being displayed
body: <p>I am just a beginner of ASP.NET MVC. I just started with creating my login page it works fine but I have a problem when the user inputs wrong credentials. This is what I have done:</p>
  
  <p><strong>UserProfile</strong></p>
  
  <pre><code>public partial class UserProfile
      {
          public int UserId { get; set; }
          [Display(Name = "User name")]
          [Required(ErrorMessage = "Username is required.")]
          public string UserName { get; set; }
          [Display(Name = "Password")]
          [DataType(DataType.Password)]
          [Required(ErrorMessage = "Password is required.")]
          public string Password { get; set; }
          public bool IsActive { get; set; }
      }
  </code></pre>
  
  <p><strong>HomeController:</strong></p>
  
  <pre><code>public class HomeController : Controller
  {
      public ActionResult Login()
      {
          return View();
      }
  
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Login(UserProfile objUser)
      {
          if (ModelState.IsValid)
          {
              using (DB_Entities db = new DB_Entities())
              {
                  var obj = db.UserProfiles.Where(a =&gt; a.UserName.Equals(objUser.UserName) &amp;&amp; a.Password.Equals(objUser.Password)).FirstOrDefault();
                  if (obj != null)
                  {
                      Session["UserID"] = obj.UserId.ToString();
                      Session["UserName"] = obj.UserName.ToString();
                      return RedirectToAction("UserDashBoard");
                  }
              }
          }
          else
          {
              ModelState.AddModelError("", "Invalid Credentials");
          }
          return View(objUser);
      }
  
      public ActionResult UserDashBoard()
      {
          if (Session["UserID"] != null)
          {
              return View();
          }
          else
          {
              return RedirectToAction("Login");
          }
      }
  }
  </code></pre>
  
  <p>And the <strong>View</strong></p>
  
  <pre><code>@model MyWebApplication.Models.UserProfile
  
  @{
      ViewBag.Title = "Login";
  }
  
  @using (Html.BeginForm("Login", "Home", FormMethod.Post))
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.UserName, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.UserName, "", 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;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Login" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>
  
  <p>I am not sure why the message "Invalid Credentials" is not being displayed when the user types a username that isn't stored in the database.</p>