title: Remote Validation of Uniqueness in ASP MVC
body: <p>Using ASP MVC and SQL Server, I am trying to test the existence of a client before creating him via the uniqueness of his Email's value.</p>
  
  <p>Following a lot of tutorials and solutions, I didn't success to resolve this problem. This is what I've tried :</p>
  
  <p><strong>ClientController :</strong></p>
  
  <pre><code> [HttpPost]
      public ActionResult Create(Client cmodel)
      {
          try
          {
              if (ModelState.IsValid)
              {
                  ClientManagement cdb = new ClientManagement();
                  if (cdb.AddClient(cmodel))
                  {
  
                      ViewBag.Message = "Client Details Added Successfully";
                      ModelState.Clear();
                  }
              }
              return RedirectToAction("Index");
          }
          catch
          {
              return View();
          }
      }
  
   public JsonResult IsClientExist(string Email)
      {
  
          List&lt;Client&gt; cm = new List&lt;Client&gt;();
  
          return Json(!cm.Any(x =&gt; x.Email == Email), JsonRequestBehavior.AllowGet);
      }
  </code></pre>
  
  <p><strong>Class ClientManagement :</strong></p>
  
  <pre><code> public bool AddClient(Client cmodel)
      {
          connection();
          SqlCommand cmd = new SqlCommand("AddNewClients", con);
          cmd.CommandType = CommandType.StoredProcedure;
  
          cmd.Parameters.AddWithValue("@Email", cmodel.Email);
          cmd.Parameters.AddWithValue("@Password", cmodel.Password);
  
          con.Open();
          int i = cmd.ExecuteNonQuery();
          con.Close();
  
          if (i &gt;= 1)
              return true;
          else
              return false;
      }
  </code></pre>
  
  <p><strong>Model Client :</strong></p>
  
  <pre><code> public class Client
  {
  
      [Display(Name = "Email")]
      [Required(ErrorMessage = "Email is required.")]
      [EmailAddress(ErrorMessage = "Invalid Email Address")]
      [DataType(DataType.EmailAddress)]
      [StringLength(30)]
      [Remote("IsClientExist", "Client", ErrorMessage = "Email is already exists in Database.")]
      public string Email { get; set; }
  
      [Display(Name = "Password")]
      [DataType(DataType.Password)]
      [Required(ErrorMessage = "Password is required.")]
      public string Password { get; set; }
  }
  </code></pre>
  
  <p><strong>View Create :</strong></p>
  
  <pre><code>&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;script src="~/Scripts/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
      &lt;script src="~/Scripts/jquery.validate.min.js"&gt;&lt;/script&gt;
     &lt;script src="~/Scripts/jquery.validate.unobtrusive.min.js"&gt;&lt;/script&gt;
  </code></pre>