2015-04-23 23 views
1

我有一个使用模态引导的部分视图登录。在客户端验证了模型后,所有工作都正常。但是我想检查登录是否失败,如果是,请将错误消息从控制器返回到模态引导,而不是返回页面。我怎样才能做到这一点?如何从控制器进行模态引导验证?

我_partialview登录

<div id="loginModal" class="modal fade"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-body"> 
       @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal"})) 
       { 
        @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">Submit</button> 
        </div> 
       } 
    </div> 
</div> 

账户控制器

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 
     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 View(model); 
     } 
    } 

回答

1

如果我理解你想获得的错误,他们展现给用户无需刷新页面。所有你需要的是使用一些Ajax,然后使用它的成功/错误功能显示错误/重定向到新页面。堆栈上也有很多例子。请记住,出于安全原因和密码使用HTTPS。这里的一些例子:Login Page using ASP.Net & Ajax

相关问题