根据我的理解,我认为远程验证会在用户离开文本框后立即调用控制器操作。我正在尝试验证用户名(电子邮件)但仅在我提交表单时调用控制器操作(当我单击提交按钮时)。我想在离开电子邮件文本框后立即调用此功能。我在这里做错了什么?
视图模型
public class RegisterViewModel
{
[Required(ErrorMessage = "Email is required")]
[DataType(DataType.EmailAddress)]
[Remote("IsEmailAvailable","Register", ErrorMessage = "Email is already registered, use forgot password to reset your password if this is the case.")]
[Display(Name = "Email")]
public string Username { get; set; }
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "LastName is required")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is required")]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is required")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "Passwords do not match")]
[Display(Name = "Confirm Password")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Business name is required")]
[Display(Name = "Business Name")]
public string BusinessName { get; set; }
[Required(ErrorMessage = "Address Line 1 is required")]
[Display(Name = "Address Line 1")]
public string AddressLine1 { get; set; }
[Display(Name = "Address Line 2")]
public string AddressLine2 { get; set; }
[Required(ErrorMessage = "Postcode is required")]
[Display(Name = "Postcode")]
public string PostCode { get; set; }
[Required(ErrorMessage = "City is required")]
[Display(Name = "City")]
public string City { get; set; }
[Required(ErrorMessage = "Country is required")]
[Display(Name = "Country")]
public string Country { get; set; }
[Required(ErrorMessage = "Telephone number is required")]
[Display(Name = "Telephone Number")]
public string TelephoneNumber { get; set; }
public bool EmailVerified { get; set; }
public bool TelephoneNumberVerified { get; set; }
}
控制器操作
[HttpGet]
public JsonResult IsEmailAvailable(string username)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
查看
@using (Html.BeginForm("Register", "Register", FormMethod.Post, new { id = "frmRegister", @class = "frmRegister" }))
{
@Html.AntiForgeryToken()
<h2>Register</h2>
@Html.ValidationSummary(false, "Some informations you have provided are not correct", new {@class = "alert alert-danger"})
<div class="form-group">
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName, null, new {id = "txtFirstName", @class = "form-control", placeholder = "First Name"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName, null, new {id = "txtLastName", @class = "form-control", placeholder = "Last Name"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.Username)
@Html.TextBoxFor(m => m.Username, null, new {id = "txtEmail", @class = "form-control", placeholder = "Email", type="email"})
@Html.ValidationMessageFor(m => m.Username)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password)
@Html.TextBoxFor(m => m.Password, null, new {id = "txtPassword", @class = "form-control", placeholder = "Password", type="password"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword)
@Html.TextBoxFor(m => m.ConfirmPassword, null, new {id = "txtConfirmPassword", @class = "form-control", placeholder = "Password", type = "password"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.BusinessName)
@Html.TextBoxFor(m => m.BusinessName, null, new { id = "txtBusinessName", @class = "form-control", placeholder = "Business Name" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.AddressLine1)
@Html.TextBoxFor(m => m.AddressLine1, null, new {id = "txtAddressLine1", @class = "form-control", placeholder = "Address Line 1"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.AddressLine2)
@Html.TextBoxFor(m => m.AddressLine2, null, new {id = "txtAddressLine1", @class = "form-control", placeholder = "Address Line 2" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.PostCode)
@Html.TextBoxFor(m => m.PostCode, null, new {id = "txtPostcode", @class = "form-control", placeholder = "Postcode"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.City)
@Html.TextBoxFor(m => m.City, null, new {id = "txtCity", @class = "form-control", placeholder = "City"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.Country)
@Html.TextBoxFor(m => m.Country, null, new {id = "txtCountry", @class = "form-control", placeholder = "Country"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.TelephoneNumber)
@Html.TextBoxFor(m => m.TelephoneNumber, null, new {id = "txtTelephoneNumber", @class = "form-control", placeholder = "Telephone Number"})
</div>
<button type="submit" class="btn btn-success btnRegister">Register</button>
}
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryval")
@Styles.Render("~/Content/css")