title: Why when Check if the user already exists in ASP.NET MVC database first not working?
body: <p>I checked more than one post in this site and tried more than one solution but still I cannot check the user exist or not when register new user , I tried the following code : </p>
  
  <pre><code>[HttpPost]
  public ActionResult register(Registration reg)
          {
              if (ModelState.IsValid)
              {
                  var userexist = db.Registration.Any(x =&gt; x.username == reg.username);
                  if (userexist)
                  {
                      ModelState.AddModelError("username", "User with this name already exists");
                      return View(reg);
                  }
                  else
  
                  {
                      db.Registration.Add(reg);
                      db.SaveChanges();
                      return RedirectToAction("Index");
                  }
  
  
              }
              return View();
          }
  </code></pre>
  
  <p>this is registration model :</p>
  
  <pre><code>namespace registration.Models
  {
      using System;
      using System.Collections.Generic;
      using System.ComponentModel.DataAnnotations;
  
      public partial class Registration
      {
          public int Userid { get; set; }
  
          [Required]
          [Display(Name ="ID or Iqama No ")]
          public string username { get; set; }
  
          [Required]
          [Display(Name = "Medical Record Number ")]
          public int PatientNo { get; set; }
  
          [Required]
          [Display(Name = "Mobile ")]
          public string Mobile { get; set; }
  
  
          [Display(Name = "Email Address ")]
          [DataType(DataType.EmailAddress)]
          public string Email { get; set; }
          [Required]
          [DataType(DataType.Password)]
          public string Password { get; set; }
      }
  </code></pre>
  
  <p>this is the view code and submit button there create button: </p>
  
  <pre><code>@model registration.Models.Registration
  
  @{
      ViewBag.Title = "Register New User";
  }
  
  &lt;h2&gt;register&lt;/h2&gt;
  
  @using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Registration Window&lt;/h4&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 { @type = "number", @min = "0", @value = "0", @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.PatientNo, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.PatientNo, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.PatientNo, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Mobile, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Email, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Email, "", 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="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Login")
  &lt;/div&gt;
  </code></pre>
  
  <p>what is the missing in my code why its not working when click enter or tab or by mouse click its not checking ? </p>