1

I have been banging my head against the wall trying to solve this issue.

I have this view controller

public class LoginController : Controller
 {
 public ActionResult Index(LoginClass model)
 {
 return View(model);
 }
 [HttpPost]
 public ActionResult Login(LoginClass model, string ReturnUrl)
 {
 if (!this.ModelState.IsValid)
 {
 return this.View(model);
 }
 if (ModelState.IsValid)
 {
 if (Membership.ValidateUser(model.UserName, model.Password))
 {
 FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
 if (Url.IsLocalUrl(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/")
 && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\"))
 {
 return Redirect(ReturnUrl);
 }
 else
 {
 return RedirectToAction("Index", "Home");
 }
 }
 else
 {
 ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect");
 }
 }
 return RedirectToAction("Index", "Login", model);
 }
 public ActionResult Logout()
 {
 FormsAuthentication.SignOut();
 return RedirectToAction("Index", "Home");
 }
 }

and here is my view:

<section id="login">
 <div class="container">
 <form action="~/Login/Login" id="Login" method="post">
 <div class="row">
 <div class="col-md-12">
 <p>
 <label for="username">Username</label>
 <input type="text" id="username" name="username" class="form-control" />
 </p>
 </div>
 </div>
 <div class="row">
 <div class="col-md-12">
 <p>
 <label for="password">Password</label>
 <input type="password" id="password" name="password" class="form-control" />
 </p>
 </div>
 </div>
 <div class="row">
 <div class="col-md-12">
 <p>
 <input type="submit" id="submit" name="submit" value="Login" class="btn btn-default" />
 </p>
 </div>
 </div>
 <div class="row">
 <div class="col-md-12">
 <p>
 <label for="password">Remember Me?</label>
 <input type="checkbox" id="chkPersist" name="chkPersist" />
 </p>
 </div>
 </div>
 <div class="row">
 <div class="col-md-12">
 <p>
 @Html.ValidationSummary()
 </p>
 </div>
 </div>
 </form>
 </div>
</section>

My issue is that my error message is not appearing when I enter the wrong username and password. Why is not displaying?

asked Apr 5, 2016 at 19:11
2
  • what happens when you do @Html.ValidationSummary(false) Commented Apr 5, 2016 at 19:27
  • Same result, no error message appears Commented Apr 5, 2016 at 19:48

2 Answers 2

1

The problem is with the last line in your Login method. You are calling RedirectToAction instead of View. This has the consequence that you lose all your view specific state including the model state and validation errors that you built up in your Login Action. You can change your Login method like so (I simplified it a little), really the only change is replacing RedirectToAction("Index", "Login", model) with View(model) on the last line.

If you do want to redirect in the event of authentication failure and you did want to use RedirectToAction then see my other answer I posted here.

[HttpPost]
public ActionResult Login(LoginClass model, string ReturnUrl)
{
 if (!this.ModelState.IsValid)
 return this.View(model);
 if (Membership.ValidateUser(model.UserName, model.Password))
 {
 FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
 if (Url.IsLocalUrl(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/")
 && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\"))
 {
 return Redirect(ReturnUrl);
 }
 else
 {
 return RedirectToAction("Index", "Home");
 }
 }
 ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect");
 return this.View(model); // return to the same view to show your error message
 // return RedirectToAction("Index", "Login", model); // do not redirect
}
answered Apr 8, 2016 at 10:15
Sign up to request clarification or add additional context in comments.

Comments

1

You need to add a placeholder by using the Html Helper function to display the message.

 <div>@Html.ValidationMesssage("KeyName")</div>

to your view so that you can display the validation message.

The KeyName comes from controller

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

Also you may need to ensure client side validation is enabled in your web.config.(usually they are)

 <appSettings>
 <add key="ClientValidationEnabled" value="true" />
 <add key="UnobtrusiveJavaScriptEnabled" value="true" />
 </appSettings>
answered Apr 5, 2016 at 19:22

5 Comments

what happens when you do @Html.ValidationSummary(false)
same result...nothing
i guess another thing to check is whether jquery is available on that page?
jquery is available on the page and the items in my web config are there.
i am not seeing anything wrong with your code. Can you try moving your @Html.ValidationSummary code line as given in the example here geekswithblogs.net/stun/archive/2011/01/28/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.