显示错误的密码信息

145*_*446 3 .net validation razor c#-4.0 asp.net-mvc-3

我的下面是用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>
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 8

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

ModelState.AddModelError("password", "The username or password is incorrect");
Run Code Online (Sandbox Code Playgroud)

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

ModelState.AddModelError("", "The user name or password provided is incorrect.");
Run Code Online (Sandbox Code Playgroud)

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

@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
Run Code Online (Sandbox Code Playgroud)