显示错误的密码信息

时间:2012-06-11 17:15:12

标签: .net asp.net-mvc-3 validation c#-4.0 razor

我的下面是用mvc3 razor编写的表格。我想在用户输入错误密码时显示一条消息。我已经放置了一个标签来显示消息,但不知道如何在mvc中完成show / hide。请帮忙。

@using (Html.BeginForm("Authenticate", "Authorization", FormMethod.Post, new { autocomplete = "off" }))
{
    < div>
    < fieldset>
        <legend > User Information </legend>

        <div class="editor-label">
            @Html.Label("Password")

            @Html.TextBox("password")
            @Html.ValidationMessageFor(m => m.password)
            @Html.Label("Incorrect Password")
        </div>
        <p>
            @Html.Hidden("tab", ViewData["tab"])
            <input type="submit" value="Submit" />
        </p>

    </fieldset>
</div>
}

1 个答案:

答案 0 :(得分:8)

在应该验证凭据的控制器操作中,您可以添加模型状态错误:

ModelState.AddModelError("password", "The username or password is incorrect");

使用Visual Studio中的内置向导创建一个新的ASP.NET MVC应用程序,然后导航到~/Controller/AccountController.cs。然后查看LogOn POST操作。当凭据无效时,它会添加一般的模型状态错误:

ModelState.AddModelError("", "The user name or password provided is incorrect.");

并在视图中使用ValidationSummary助手显示此错误:

@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")