0

Ich benutze bootstrap modalen Dialog für Login-Popup, Probleme mit der Anzeige von Fehlern auf Etikett auf modalen Dialog platziert. Alles funktioniert gut, der Controller wird aufgerufen und überprüft die Benutzerdaten. Das Problem tritt auf, wenn die JSON-Antwortnachricht vom Controller zurückgesendet wird. Anstatt einen Fehler im modalen Dialog anzuzeigen, wird auf die leere Seite umgeleitet und der JSON-Fehler oben angezeigt. Bitte beraten oder vorschlagen. Bitte senden Sie mir keine doppelten Fragen Link Ich habe schon viel recherchiert. Überprüfen Sie den angegebenen Code und sehen Sie, was falsch ist.Anzeige Login Validierung Fehler auf Bootstrap Modal Pop-up funktioniert nicht

Teilansicht: _signIn.cshtml

<div class="modal fade" id="signInModal" tabindex="-1" role="dialog" aria-labelledby="signInModalLabel" aria-hidden="true" 
data-keyboard="false"> 
<div class="modal-dialog modal-sm"> 
      <div class="modal-content"> 
     <div class="modal-body"> 
      <!-- Row --> 
      <div class="row"> 
       <h4> 
        <p class="text-xs-center"> 
         Sign in to Photolab 
        </p> 
       </h4> 
      </div> 

      <p class="text-xs-center"> 
       <span class="pi-or">or use your email address</span> 
      </p> 

      @using (Html.BeginForm("ValidateUser", "Account", 
FormMethod.Post, new { Id = "signInForm" })) 
      { 

       @Html.AntiForgeryToken() 
       @Html.ValidationSummary(true) 

       <div class="row"> 
        <div class="col-md-12 col-sm-10 col-xs-12"> 

         <div class="modalbox"> 

          <p> 
           <label id="lblMessage" class="small"></label> 
          </p> 

          <!-- Email form --> 
          <div class="form-group"> 
           @Html.TextBoxFor(model => model.UserName, new { @class = "form-control form-control-round", @required = "required", @placeholder = "Email address", id = "signInEmail" }) 
           @Html.ValidationMessageFor(model => model.UserName) 
          </div> 
          <!-- End email form --> 
          <!-- Password form --> 
          <div class="form-group"> 
           @Html.TextBoxFor(model => model.Password, new { @class = "form-control form-control-round", @type = "Password", @placeholder = "Password", @required = "required" }) 
          </div> 
          <!-- End password form --> 

          <p class="pull-right small"> 
           <a href="#" id="forgotModal"> 
            Forgot password? 
           </a> 
          </p> 

          <div class="checkbox pull-left"> 
           <label class="small"> 
            @Html.CheckBoxFor(model => model.RememberMe)Remember Me 
           </label> 
          </div> 
          <!-- Submit button --> 
          <div class="form-group-btn"> 
           <button type="submit" id="btnSignIn" class="btn btn-primary btn-block"> 
            Sign In 
           </button> 
          </div> 
          <!-- End submit button --> 

         </div> 
         <!-- End box --> 
        </div> 

       </div> 

      } 
      <!-- End row --> 
     </div> 
    </div> 
    <div class="modal-footer"> 
     <p class="text-xs-center"> 
      Don't have Account? <a href="#" id="btnSignUp" class="flipLink">Sign Up</a> 
     </p> 
    </div> 
</div> 

JQuery:

<script> 
$(function() { 

    $('#btnSignIn').click(function() { 

     var url = '@Url.Action("ValidateUser", "Account")'; 
     event.preventDefault(); 
     $.ajax({ 
      type: 'POST', 
      cache:false, 
      url: url, 
      datatype:'json', 
      data: $(this).serialize(), 
      success: function (result) { 
       if (result.success != true) { 
        lert(result.response); 
        $("#lblMessage").text(""); 
        $("#lblMessage").append(result.response); 
       } 
       else 
       { 
        alert(result.response); 
        window.location.href = result.response; 
       } 
      } 
     }); 
    }); 
} 

MVC AccountController.cs

[AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult ValidateUser(LoginModel model, string returnUrl) 
    { 
     var result = new { success = "false", response = "Error Message" }; 

     try 
     { 
      var loginResult = _customerRegistrationService.ValidateCustomer(model.UserName, model.Password); 

      switch (loginResult) 
      { 
       case LoginResult.Successful: 
        { 
         //_authenticationService.SignIn(model.UserName, model.Password, true); 

         ImplementFormsAuthentication(model.UserName); 

         result = new { success = "true", response = Url.Action("Listings", "Listing") }; 

         return RedirectToAction("Listings", "Listing"); 

         //break; 

        } 
       case LoginResult.NotRegistred: 

        result = new { success = "false", response = "The user name or password provided is incorrect" }; 
        break; 
       case LoginResult.WrongPassword: 
        result = new { success = "false", response = "Incorrect Password" }; 
        break; 

       default: 
        result = new { success = "false", response = "Invalid username/password" }; 
        break; 
      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

     return Json(result, JsonRequestBehavior.AllowGet); 
    } 

_Layout.cshtml

<li class="nav-item"> 
     <a href='#' id="btnLogin" class="nav-link active" data-toggle="modal" data-target="#signInModal">             Login 
              </a> 
             </li> 

Antwort

2

Sie Knopf einreichen werden. Es wird also auf eine leere Seite umgeleitet. Verwenden Sie die Schaltfläche "Schaltfläche", damit die Seite nicht in eine leere Seite umgewandelt wird.

<button type="button" id="btnSignIn" class="btn btn-primary btn-block"> 
     Sign In 
</button> 
+0

Danke! Es klappt , – Sidh