I'm currently working on a registration page where the users enter their email address. I want every email to be unique.
This is a part of my RegisterModel:
[Required()]
[System.Web.Mvc.Remote("IsUserEmailAvailable", "Account")]
[EmailAddress()]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
public string Email { get; set; }
..
This is a part of my accountcontroller:
public JsonResult IsUserEmailAvailable(string UserEmail)
{
return Json(!db.UserProfiles.Any(User => User.UserName == UserEmail), JsonRequestBehavior.AllowGet);
}
This my my view:
<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<hgroup class="title">
<h1>Create Account</h1>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.ConfirmEmail)
@Html.TextBoxFor(m => m.ConfirmEmail)
</li>
</ol>
<input type="submit" value="Registrera" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
If I remove the [System.Web.Mvc.Remote("IsUserEmailAvailable", "Account")] everything is fine except the emailaddress wont be unique. With it, when I press the Submit, nothing happens.
Have I missed anything?
@Daniel J.G. is correct. Adding [AllowAnonymous] to the function makes it accessible.