2014-11-01 17 views
2

我使用MVC 4和用于帐户处理的SimpleMembership。当用户登录到网页时,我正在使用Boostrap V3.2.0模式。模式工作正常,甚至通过“Ajax.BeginForm”处理Modelstate验证我遇到的问题是在modelstate验证后,如果登录失败(用户输入的用户名或密码不正确),Account控制器的Login ActionResult返回登录页面的部分视图。它不是在Modal中加载部分视图,而是加载为整页(仍然是partialview,没有_Layout页面)。如何在登录失败时将局部视图加载回登录模式?MVC 4:在验证失败时返回引导模式内部的局部视图

这里是我的模态登录视图代码:

@model AdorationSuite.Models.LoginModel 

<script type="text/javascript" src="/scripts/jquery.validate.min.js"></script> 
<script type="text/javascript" src="/scripts/jquery.validate.unobtrusive.min.js"></script> 

@{AjaxOptions options = new AjaxOptions(); 
options.HttpMethod = "POST"; 
options.Url = Url.Action("Login", "Account"); 
options.UpdateTargetId = "modal-body"; 
options.InsertionMode = InsertionMode.Replace; 
} 

@using (Ajax.BeginForm("Login", "Account", options, new {id="edit-form"})) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(false, "Come on now, get it together man!!") 

    <div class="modal-header" style="height:auto"> 
     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
     <h4 class="modal-title" id="ModalLabel">Log In</h4> 
    </div> 

    Html.RenderPartial("~/Views/Account/Partials/LoginForm.cshtml", Model); 

    <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     <button type="submit" id="submit-login" value="Login" class="btn btn-primary">Login</button> 
    </div> 
} 

这里是在我的控制器代码:

[HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public ActionResult Login(LoginModel model, string returnUrl) 
     { 
      if (ModelState.IsValid && WebSecurity.Login(model.EmailAddress, hashPassword(model.Password), persistCookie: model.RememberMe)) 
      { 
       return RedirectToLocal(returnUrl); 
      } 

      // If we got this far, something failed, redisplay form 
      ModelState.AddModelError("", "The email address or password provided is incorrect."); 

      return PartialView("Login", model); 
     } 

时,如果用户不出现例如一个ModelState中的错误输入用户名或密码,然后表单验证正常工作,模态更新和验证错误。但是,如果ModelState中验证通过,但登录失败,当控制器返回以下内容:

return PartialView("Login", model); 

这部分观点的负载为一个完整的页面(页面_layout外),而不是在登录模式。

有没有什么明显的我失踪了,为什么在失败的登录结果不回发给登录模态?

在此先感谢。

回答

2

我想你会需要一些客户端代码。

尝试:

<div id="partialSummaryDiv"> Html.RenderPartial("~/Views/Account/Partials/LoginForm.cshtml", Model);</div> 

事件你有你的Ajax调用

$.ajax({ 
url: "/Controller/Action", 
type: "POST", 
success: function (result) { 
    // refreshes partial view 
    $('#partialSummaryDiv').html(result); 
} 
}); 
+0

@McKeymaker你可以看到这个,请...想你[http://stackoverflow.com/questions/ 36132932 /附加使用的Ajax式模态数据-ASP净MVC] – aboamin12 2016-03-21 21:34:06