我有一个网页,允许用户更新员工的拳头姓名,姓氏,电子邮件地址和电话号码。我在如何检查用户电子邮件地址是否已更新时遇到一些麻烦,如果是,那么新的电子邮件地址是否已在数据库中使用。
此外,如果电子邮件地址未更新,请在提交时将其保留在系统中(如果他们的电话号码已更新。
任何帮助都有帮助!
已更新
控制器
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; }
}
答案 0 :(得分:3)
在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;}