title: How to Remote validate email or check if email exists
body: <p>I am trying to check if Email exists at registration so that there are no duplicate emails in the database. I am doing this with the MVC4 default Internet Application but i have added an Email field to the RegisterviewModel. I also noticed that, this is well done with UserName field but i have no idea how they did it. I tried to follow This blog below but no luck as there is no response when i click create. <a href="http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation" rel="nofollow">Tug Berk</a>. When i use firebug i am getting <code>Status: 302 Found</code> when <code>Json action excutes</code></p>
  
  <p>This is what i have have:</p>
  
  <p><strong>UserProfile</strong></p>
  
  <pre><code>[Table("UserProfile")]
  public class UserProfile
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int UserId { get; set; }
      public string UserName { get; set; }
      public string Email { get; set; }
  }
  </code></pre>
  
  <p><strong>RegisterViewModel</strong></p>
  
  <pre><code>public class RegisterModel
  {
      [Required]
      [Display(Name = "User name")]
      public string UserName { get; set; }
  
      [Required]
      [DataType(DataType.EmailAddress)]
      [Display(Name = "Email")]
      [Remote("DoesUserEmailExist", "Account", HttpMethod = "POST", ErrorMessage = "Email address already exists. Please enter a different Email address.")]
      public string Email { get; set; }
  
      [Required]
      [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.",      MinimumLength = 6)]
      [DataType(DataType.Password)]
      [Display(Name = "Password")]
      public string Password { get; set; }
  
      [DataType(DataType.Password)]
      [Display(Name = "Confirm password")]
      [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
      public string ConfirmPassword { get; set; }
  }
  </code></pre>
  
  <p><strong>Register Action</strong></p>
  
  <pre><code>    [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public ActionResult Register(RegisterModel model)
      {
          if (ModelState.IsValid)
          {
              try //CreateUserAndAccount
              {
                  WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { model.Email });
                  WebSecurity.Login(model.UserName, model.Password);
                  return RedirectToAction("Index", "Home");
  
              }
              catch (MembershipCreateUserException e)
              {
                  ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
              }
          }
  
          // If we got this far, something failed, redisplay form
          return View(model);
      }
  </code></pre>
  
  <p><strong>Json Action</strong></p>
  
  <pre><code>[HttpPost]
      public JsonResult DoesUserEmailExist(string email)
      {
  
          var user = Membership.GetUserNameByEmail(email);
  
          return Json(user == null);
      }
  </code></pre>
  
  <p><strong>Edit: Added Register View</strong></p>
  
  <pre><code>     @model Soccer.Models.RegisterModel
  
       @{
       ViewBag.Title = "Register";
       }
  
       &lt;hgroup class="title"&gt;
       &lt;h1&gt;@ViewBag.Title.&lt;/h1&gt;
       &lt;h2&gt;Create a new account.&lt;/h2&gt;
       &lt;/hgroup&gt;
  
   @using (Html.BeginForm()) {
   @Html.AntiForgeryToken()
   @Html.ValidationSummary()
  
   &lt;fieldset&gt;
      &lt;legend&gt;Registration Form&lt;/legend&gt;
      &lt;ol&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.UserName)
              @Html.TextBoxFor(m =&gt; m.UserName)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.Email)
              @Html.TextBoxFor(m =&gt; m.Email)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.Password)
              @Html.PasswordFor(m =&gt; m.Password)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.ConfirmPassword)
              @Html.PasswordFor(m =&gt; m.ConfirmPassword)
          &lt;/li&gt;
      &lt;/ol&gt;
      &lt;input type="submit" value="Register" /&gt;
  &lt;/fieldset&gt;
   }
  
   @section Scripts {
   @Scripts.Render("~/bundles/jqueryval")
   }
  </code></pre>
  
  <p>Is there something i shouldn't be doing or is there another simpler option i could take here?</p>