I have an email property in my model that looks like this
[Required(ErrorMessage ="Email Address is required.")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
[Remote("checkEmailValidate", "Employee", HttpMethod = "POST", ErrorMessage ="User with this Email already exists")]
public virtual string EmailAddress { get; set; }
And I have two different views that use this model, the first is when creating a user;
<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>
And the second is when you are editing the users account
<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" } })
@Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
</div>
</div>
Now my controller looks something like this
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "EmployeeId,FirstName,LastName,UniversityID,EmailAddress,Department,MinimumHours,ApproverEmailAddress,IsActive,Type,NotificationRecipients,ReviewerFor,IsDepartmentHR,IsAdministrator")] Employee employee)
{
if (checkEmail(employee.EmailAddress))
{
if (IsAuthorized(AuthorizationType.Either))
{
if (ModelState.IsValid)
{
if (IsAuthorized(AuthorizationType.Administrator))
{
db.Employees.Add(employee);
db.SaveChanges();
Utilities.AuditEvent(loggedOnEmployee, employee, Utilities.AuditEventType.AddEmployee);
return RedirectToAction(actionName: "Index");
}
if (IsAuthorized(AuthorizationType.DepartmentHR))
{
employee.Department = loggedOnEmployee.Department;
db.Employees.Add(employee);
db.SaveChanges();
Utilities.AuditEvent(loggedOnEmployee, employee, Utilities.AuditEventType.AddEmployee);
return RedirectToAction(actionName: "Index");
}
}
return View(employee);
}
else
{
return RedirectToAction(actionName: "Restricted");
}
}
else
{
return RedirectToAction(actionName: "Create");
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "EmployeeId,FirstName,LastName,UniversityID,EmailAddress,Department,MinimumHours,ApproverEmailAddress,IsActive,Type,NotificationRecipients,ReviewerFor,IsAdministrator,IsDepartmentHR")] Employee employee)
{
if (IsAuthorized(AuthorizationType.Either))
{
if (ModelState.IsValid)
{
if (IsAuthorized(AuthorizationType.Administrator))
{
Employee employeeInDb = db.Employees.Find(employee.EmployeeId);
db.Entry(employeeInDb).CurrentValues.SetValues(employee);
db.Entry(employeeInDb).State = EntityState.Modified;
db.SaveChanges();
Utilities.AuditEvent(loggedOnEmployee, employeeInDb, Utilities.AuditEventType.EditEmployee);
return RedirectToAction(actionName: "Index");
}
if (employee.Department != loggedOnEmployee.Department)
{
return RedirectToAction(actionName: "Restricted");
}
else
{
Employee employeeInDb = db.Employees.Find(employee.EmployeeId);
db.Entry(employeeInDb).CurrentValues.SetValues(employee);
db.Entry(employeeInDb).State = EntityState.Modified;
db.SaveChanges();
Utilities.AuditEvent(loggedOnEmployee, employeeInDb, Utilities.AuditEventType.EditEmployee);
return RedirectToAction(actionName: "Index");
}
}
return View(employee);
}
else
{
return RedirectToAction(actionName: "Restricted");
}
}
This is my problem:
I want to make sure that a user isn't created using an existing email address, but I want to be able to edit existing users. What happens now is when creating a user, it works perfectly, but when editing a user, I still get the user already exists message. How do I fix this so that the warning doesn't pop-up on the edit view?