2017-02-16 3 views
0

Ich habe ein Login form in einem #myModal Körper. Die Anmeldefunktion ist in Ordnung, außer dass bei Eingabe des falschen Benutzernamens und Passworts keine Weiterleitung an die #myModal mit den entsprechenden Fehlermeldungen erfolgt. Stattdessen wird auf die Standardseite login umgeleitet. Ich weiß nicht, wie man auf die modal statt der Standardseite login umleiten kann.MVC C# Umleiten zu Login Modal

Modal

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
<div class="modal-dialog" role="document"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
      <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
     </div> 
     <div class="modal-body"> 
      <section id="loginForm"> 
       @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
       { 
        @Html.AntiForgeryToken() 
        <h4>Use a local account to log in.</h4> 
        <hr /> 
        @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
        <div class="form-group"> 
         @Html.Label("E-mail", new { @class = "col-md-2 control-label" }) 
         <div class="col-md-10"> 
          @Html.TextBox("email", "", new { @class = "form-control", @placeholder = "Email" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         @Html.Label("Password", new { @class = "col-md-2 control-label" }) 
         <div class="col-md-10"> 
          @Html.Password("Password", "", new { @class = "form-control", @placeholder = "Password" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         <div class="col-md-offset-2 col-md-10"> 
          <input type="submit" value="Log in" class="btn btn-success" /> 
         </div> 
        </div> 
       } 
      </section> 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     </div> 
    </div> 
</div> 

Anmeldung Controller-Methode

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (!ModelState.IsValid) 
     { 
      //how do I redirect back to the login modal if modelstate is invalid? 
      return View(model); 
     } 

     // This doesn't count login failures towards account lockout 
     // To enable password failures to trigger account lockout, change to shouldLockout: true 
     var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 
     switch (result) 
     { 
      case SignInStatus.Success: 
       return RedirectToLocal(returnUrl); 
      case SignInStatus.LockedOut: 
       return View("Lockout"); 
      case SignInStatus.RequiresVerification: 
       return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
      case SignInStatus.Failure: 
      default: 
       ModelState.AddModelError("", "Invalid login attempt."); 
       return View(model); 
       //how do I redirect back to the login modal? 
     } 
    } 
+0

können Sie es mit Ajax tun –

Antwort

0

Leider kann man nicht eine modale umleiten. Ein Modal ist Teil der Seite/Ansicht. Obwohl Sie auf die Login-Schaltfläche im Modal klicken, führt diese Schaltfläche einen POST aus, der eine Umleitung der gesamten Seite zur Folge hat. Sie müssen Ihren Login-Controller entweder über Ajax per POST anmelden oder eine Weiterleitung in Ihren Bewerbungsablauf integrieren.