我有一個帳戶控制器,我想當我輸入一個不正確的模式引導用戶名/密碼它會返回一條消息「無效的登錄嘗試。」從ActionResult Login()到模態。如何從服務器端驗證模態引導?
我_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>
我的控制器:
[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);
}
}
你有沒有考慮過使用Ajax.BeginForm,然後返回你的部分模態視圖進入頁面。 –
@Symeon我嘗試過使用Ajax.BeginForm並返回控制器中的PartialView,但是當我submmited它重定向到https:// localhost:xxxxx/Account/Login並呈現部分視圖。不顯示模式對話框。我不想重定向到另一個視圖 –
聽起來像登錄失敗,或者登錄方法不允許匿名 - 儘管您的頂部有[AllowAnonymous]標記。你有沒有在調試中試過它,看看它是否進入了Login方法? –