I have an Account
Controller and when an incorrect username/password is entered in a bootstrap modal, I want it to return a message "Invalid login attempt." from ActionResult Login()
to the modal.
My _loginPartialView:
<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<section id="loginForm">
@using (Ajax.BeginForm("Login", "Account", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "loginModal" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
</div>
<div style="text-align:right;">
<button type="submit" class="btn btn-primary btn-sm">Submmit</button>
</div>
}
</section>
</div>
</div>
</div>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script>
$(document).ready(function () {
$('.launchLoginModal').click(function () {
$('#loginModal').modal({
keyboard: false
});
});
});
</script>
My Account
Controller:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return PartialView("_LoginModalPartial", model);
}
}
First, since you're using @Ajax helpers, you should install and reference Microsoft jQuery Unobtrusive Ajax (you can find an older but still pertinent guide here: https://www.asp.net/mvc/overview/older-versions/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript).
Second, you should enable client side validation (see in the guide)
Third, you need to specify as UpdateTargetId an existing DOM element to be updated (I do not see the "loginModal" in the code sample). I believe that this id should be inside the modal html code, otherwise it will recreate the modal html and reset its status. (I would move the modal definition in the parent view and let the partial with only the form definition).
I added a comment about this, but it may help as your solution. It sounds like invalid inputs on form submission leads to a redirect with the modal content on a blank page? Since the modal is a partial view, try moving your scripts to the top of the _loginPartialView. The same may hold true for any bundles you are using in the layout page.
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<div id="loginModal" class="modal fade">
...
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With