C# 更新用户电子邮件以查看其是否保持不变或正在更新

C# 更新用户电子邮件以查看其是否保持不变或正在更新,c#,entity-framework,asp.net-mvc-5.1,C#,Entity Framework,Asp.net Mvc 5.1,我有一个网页,允许用户更新员工的名字、姓氏、电子邮件地址和电话号码。我不太明白如何检查用户的电子邮件地址是否已更新,如果已更新,新的电子邮件地址是否已在数据库中使用 此外,如果电子邮件地址没有更新,请在提交时将其保留在系统中(如果他们的电话号码已更新) 任何帮助都是有帮助的 已更新 控制器 public JsonResult isEmailAvailable(string EmailId) { return Json(!db.mvcUsers.Any(user =

我有一个网页,允许用户更新员工的名字、姓氏、电子邮件地址和电话号码。我不太明白如何检查用户的电子邮件地址是否已更新,如果已更新,新的电子邮件地址是否已在数据库中使用

此外,如果电子邮件地址没有更新,请在提交时将其保留在系统中(如果他们的电话号码已更新)

任何帮助都是有帮助的

已更新

控制器

    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);
    }
查看

 <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>

在MVC中,您可以使用System.Web.MVC中的远程属性

编写自定义方法:

public JsonResult isemailidavailable(string emailid)
{
  return Json(!db.mvcUsers.Any(mvcuser => mvcuser.emailid == emailid) ,JsonRequestBeaviour.AllowGet); ;

}
在远程属性中使用它

[Remote("isemailidavailable","Home",ErrorMessage="Email Id already Exists")]
Public string EmailId{get; set;}

好的,我用了你说的话,用了视频,它很有效。因此,在测试它时,我能够将添加到相同电子邮件地址的最后一条记录更改为以前的任何记录。这是什么原因?事实上,我从同一视频中得到了答案:),如果禁用Javascript,验证往往会失败,并且记录会被保存。
[Remote("isemailidavailable","Home",ErrorMessage="Email Id already Exists")]
Public string EmailId{get; set;}