如何在引导模式对话框中使用MVC ValidationMessageFor

时间:2018-11-01 09:30:52

标签: asp.net asp.net-mvc razor

大家好,我有一个小问题要问...我正在尝试使用ValidationMessageFor将错误消息发送到登录引导程序模式dilog中,但实际上不起作用,我也不知道为什么。

这是我的Html.BeginForm登录日志

<div class="col-xs-2 login-btn">
            <a class="btn pfologin" data-toggle="modal" data-target=".bootstrapmodal">
                <span> LOGIN</span>
            </a>

            <!-- Modal dialog -->
            <div class="modal fade bootstrapmodal">
                <div class="modal-dialog">
                    @using (Html.BeginForm("Login", "Account"))
                    {
                        <div class="modal-content modal-pfo">
                            <div class="modal-body">
                                <p>
                                    User:<br />
                                    @Html.TextBoxFor(s => s.Name, new { @class = "form-control" })
                                    @Html.ValidationMessageFor(s => s.Password, "", new { @class = "text-danger" })
                                </p>
                                <p>
                                    Password:<br />
                                    @Html.TextBoxFor(s => s.Password, new { @class = "form-control" })
                                    @Html.ValidationMessageFor(s => s.Password, "", new { @class = "text-danger" })
                                </p>
                                <p style="display: flex;">
                                    @Html.CheckBoxFor(s => s.Privacy, new { @class = "checkbox" }) &nbsp;&nbsp;&nbsp;Remember me
                                </p>
                            </div>
                            <div class="modal-footer">
                                <button data-dismiss="modal" class="btn btn-pfo">Cancel</button>
                                <input type="submit" value="Login" class="btn btn-pfo" />
                            </div>
                        </div>
                    }
                </div>
        </div>...

这是模型

public class LoginModel
{
    [Required(ErrorMessage = "Username requested")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Password requested")]
    [DataType(DataType.Password)]
    public string Password { get; set; }
    public bool Privacy { get; set; }
}

这是控制器

[HttpPost]
public async Task<ActionResult> Login(LoginModel loginModel)
{
    if (String.IsNullOrEmpty(loginModel.Name))
    {
        return ModelState.AddModelError("Name", loginModel.Name);
    }
    if (String.IsNullOrEmpty(loginModel.Password))
    {
        ModelState.AddModelError("Password", loginModel.Password);
    }

    if (ModelState.IsValid)
    {

    }
    return RedirectToAction("Index", "Home");
}

是的,我从登录名/帖子重定向到索引,但是我不认为这是问题所在……谢谢!

1 个答案:

答案 0 :(得分:1)

当您返回RedirectToAction时,您将丢失所有表单数据(以及所有验证信息)。无需重定向到索引,而是返回传入的模型的视图。

第二,因为两个属性都标记为必需,所以您无需显式检查它们是否为空或为空。根据您在模型上设置的属性,该模型已经之前经过验证,可以使用您的[HttpPost]方法。如果使用此模型返回View,将显示验证消息。这是最基本的实现,但是您可能可以摆脱:

[HttpPost]
public async Task<ActionResult> Login(LoginModel loginModel)
{
    if (ModelState.IsValid)
    {
        // Do work
        return RedirectToAction("Index", "Home");
    }
    // Else, if not valid, re-render the view with the updated information and display it to the user
    return View(loginModel);
}

More info on validation here