即使验证失败,服务器端代码也会继续

时间:2015-03-08 16:54:36

标签: asp.net-mvc unobtrusive-validation

如果注册帐户时提供的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");            
    }

我遇到的另一个问题是用户创建了两次,因此数据库中有两个用户。任何人都可以在我的代码中看到有关此问题吗?或许我可能要为这个问题打开单独的票证

1 个答案:

答案 0 :(得分:1)

即使您使用验证,控制器也不会自动执行验证。您需要使用ModelState.IsValid检查以确定您的验证是否通过:

[HttpPost]
public ActionResult RegisterNewUser(RegisteredUsersModel registerUserModel)
{
    if(ModelState.IsValid)
    {
        // this block is executed only when validation succeeds
        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");            
    }

    // this code is executed only when validation fails
    return View(RegisteredUsersModel);    // or do something else, depending on your logic
}