To start off with, I did try the information from several links including this one which I thought was the most useful. I am trying to make sure a user doesn't enter an email that is already in the database. I am trying to use the Remote attribute but from the code that I have, whenever I click the submit button, nothing happens. It's like what I have prevents the code from continuing.
Here is my code starting with part of the model. Employee.cs
[Required(ErrorMessage ="Email Address is required.")]
[Remote("checkEmailValidate", "Employee", ErrorMessage ="User with this Email already exists")]
public virtual string EmailAddress { get; set; }
and here is part of my controller. EmployeeController.cs
[AllowAnonymous]
[HttpPost]
public JsonResult checkEmailValidate(string email)
{
if (checkEmail(email))
{
return Json("Email Already Exists!", JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
public Boolean checkEmail(string email)
{
Employee employee = (from e in db.Employees
where e.EmailAddress == email
select e).FirstOrDefault();
Console.Write(employee.EmailAddress);
if (employee == null)
{
return true;
}
return false;
}
And here is a snippet from the view. I'm not sure whether I should put those scripts in there. I was trying to search around but I couldn't find a clear answer on that part, but I'm going to put it in here so you can get an idea of what I have.
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control", placeholder = "Email Address" } })
@Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
</div>
</div>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
Any help would be welcome. Again when I click on the button to submit the new user's information, nothing seems to happen. What I want to have happen is to have some red text appear over or next to the box where they entered their email telling them that it already exists.
If there is an easier or "cheaper" way to have an alert like that popup on the same page so that it wont erase the other information that they put, that would be accepted as well. Thank you!