0
如果注册帐户时提供的2个密码不匹配,则验证会捕获此密码,并通知用户它们不匹配。但用户仍在创建。如果他们提供的密码少于6个字符,情况也是如此。如果验证失败,是否必须手动告诉系统不要继续?如果是这样,怎么样?即使验证失败,服务器代码也会继续执行
我的模型看起来是这样的:
[Display(Name ="Your Email")]
[Required(ErrorMessage="You must provide a valid Email")]
[EmailAddress]
public string email { get; set; }
[Required(ErrorMessage="Select a user name", AllowEmptyStrings = false)]
[Display(Name="User name")]
public string nickName { get; set; }
[Required(ErrorMessage="Select a password", AllowEmptyStrings = false)]
[Display(Name="Password")]
[MinLength(6, ErrorMessage="The password must be at least 6 characters long")]
[DataType(DataType.Password)]
public string password { get; set; }
[Required(ErrorMessage="Type your password again")]
[Display(Name="Confirm password")]
[DataType(DataType.Password)]
[Compare("password", ErrorMessage="The two passwords do not match")]
public string confirmPassword { get; set; }
的看法是这样的:
@using (Ajax.BeginForm("RegisterNewUser", "RegisterUser", new AjaxOptions { UpdateTargetId = "updaterDiv" }))
{
@Html.LabelFor(model => model.nickName)
@Html.TextBoxFor(model => model.nickName, new { @class = "RegisterControls" })
<br />
@Html.ValidationMessageFor(model => model.nickName)
<br /><br />
@Html.LabelFor(model => model.email)
@Html.TextBoxFor(model => model.email, new { @class = "RegisterControls" })
<br />
@Html.ValidationMessageFor(model => model.email)
<br /><br />
@Html.LabelFor(model => model.password)
@Html.PasswordFor(model => model.password, new { @class = "RegisterControls", id = "firstPassword" })
<br />
@Html.ValidationMessageFor(model => model.password)
<br /><br />
@Html.LabelFor(model => model.confirmPassword)
@Html.PasswordFor(model => model.confirmPassword, new { @class = "RegisterControls" })
<br />
@Html.ValidationMessageFor(model => model.confirmPassword)
<br /><br /><br />
<hr style="color: #D5E0EE; border: solid; border-style: double;" />
<input type="submit" value="Create account" class="actionButtons" />
<br /><br />
<p style="color: red">@ViewBag.DeniedMsg</p>
和控制器:
[HttpPost]
public ActionResult RegisterNewUser(RegisteredUsersModel registerUserModel)
{
ViewBag.Header = "Register";
string msg = "";
if (!users.RegisterUser(registerUserModel.nickName, registerUserModel.email, registerUserModel.password))
{
msg = "The provided email already exists";
}
ViewBag.DeniedMsg = msg;
return PartialView("_Register");
}
还有一个问题我有,是该用户创建两次,所以他有两个在数据库中。任何人都可以在我的代码中看到这个问题吗?或者,也许我可能必须为此问题开一张单独的票据