Skip to main content
 首页 » 编程设计

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

2024年02月27日37bonelee

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

此外,如果电子邮件地址未更新,请保留提交时系统中的原样(如果他们的电话号码已更新。

任何帮助都是有帮助的!

已更新

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

查看

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

型号

    [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; } 
 } 

请您参考如下方法:

在 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;}