Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
}
like image 1000
14578446 Avatar asked Nov 01 '25 06:11

14578446


1 Answers

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.")
like image 145
Darin Dimitrov Avatar answered Nov 03 '25 22:11

Darin Dimitrov