14578446
14578446

Reputation: 1074

display incorrect password message

I have below form written in mvc3 razor. I want to display a message when user has entered incorrect password. I have placed a label to display message but dont know how to accomplish show/hide in mvc. Please help.

@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>
}

Upvotes: 3

Views: 9088

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

In the controller action that is supposed to validate the credentials you could add a modelstate error:

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

Create a new ASP.NET MVC application using the built-in wizard in Visual Studio and navigate to ~/Controller/AccountController.cs. Then look at the LogOn POST action. It adds a general modelstate error when the credentials are invalid:

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

and this error is displayed using a ValidationSummary helper inside the view:

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

Upvotes: 10

Related Questions