I have a web page that allows the user to update the employees Fist Name, Last Name, Email address and Phone number. I'm having a little trouble understand on how to go about checking if the users email address has been updated and if so, is the new email address already being used in the database.
Also with that if the email address isn't updated just leave it the way it is in the system when submitted( if their phone numbers was updated.
Any help is helpful!
UPDATED
Controller
public JsonResult isEmailAvailable(string EmailId)
{
return Json(!db.mvcUsers.Any(user => user.EmailId == EmailId),JsonRequestBehavior.AllowGet);
}
// POST: Employee/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "userID,userName,FirstName,LastName,UserPassword,EmailId,StreetAdd1,StreetAdd2,City,State,ZipCode,ConfirmPassword,PhoneNumber")] mvcUser mvcUser, bool? Resetpassword)
{
if (Resetpassword == true)
{
var pass = db.usp_GeneratePassword(10).ToList();
mvcUser.UserPassword = pass[0];
}
if (ModelState.IsValid)
{
db.Entry(mvcUser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mvcUser);
}
View
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<link href="~/Content/Site.css" rel="stylesheet" />
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>mvcUser</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.userID)
<div class="form-group" style="display: none;">
@Html.LabelFor(model => model.userName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.userName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.userName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" style="display: none;">
@Html.LabelFor(model => model.UserPassword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserPassword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserPassword, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="resetPw control-label col-md-2 ">Reset Password</label>
@*<input class="resetPw form-control text-box single-line " type="checkbox" name="FirstName" />*@
<div> @Html.CheckBox("ResetPassword",false, new { @style = "margin: 10px 15px 0;" })</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Model
[MetadataType(typeof(EmailMetaData))]
public partial class mvcUser //EmployeeViewModel
{
}
public class EmailMetaData
{
[Remote("isEmailAvailable", "Employee", ErrorMessage ="User Email is already used")]
public string EmailId { get; set; }
}
In MVC you can use Remote Attribute which is present in System.Web.MVC ,
Write a Custom Method :
Use it in remote attribute