Redirect to another view using ajax call in mvc

11,121

If you want to Redirect Page on SuccessFull login then write the redirect code on Success (ajax success). Exp:

$.ajax({
        url: window.location.pathname + '/Account/Logon',
        type: 'GET',
         })
         .success(function (result) {
                    alert("user saved successfully");

         // window.location ="";
          });
Share:
11,121
gs11111
Author by

gs11111

Updated on June 04, 2022

Comments

  • gs11111
    gs11111 almost 2 years

    I have a registration form and on successful registration, it redirects to another view. I use a submit button to insert datas to database, I want to clear the values in the current view and redirect the user to logon page in another view under same controller. The submit type is not performing the action mentioned in jquery.Please help me

    here is my code preview

    Account/register

    Account/logon

     Accountcontroller.cs
          [HttpPost]
         public ActionResult Register(RegisterModel model, FormCollection form)
          {
               try {
                    ---code
                   }
               catch {
               }
          }
    
    register.cshtml
         @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "accForm"      }))    
               {
                 @html.textboxfor(.....)
                 @html.textboxfor(.....)
                 @html.textboxfor(.....)
                 @html.textboxfor(.....)
                        ....
              <input type="submit" value="CreateAccount" class="buttonclass"               title="CreateAccount" ; onclick="SaveUser(this);" 
    
                 }
    
        function SaveUser(btnClicked) {
               alert("clear");
               document.getElementById("regname").value = '';
               document.getElementById("regusername").value = '';
               document.getElementById("regemail").value = '';
               document.getElementById("regpassword").value = '';
    
    
               document.getElementById("regpassword2").value = '';
    
               $.ajax({
                       url: window.location.pathname + '/Account/Logon',
    
                       type: 'GET',
                   })
                   .success(function (result) {
                        alert("user saved successfully");
                         });
    

    UPDATE:

       function SaveUser(btnClicked) {
    
           document.getElementById("regname").value = '';
           document.getElementById("regusername").value = '';
           document.getElementById("regemail").value = '';
           document.getElementById("regpassword").value = '';
           document.getElementById("regpassword2").value = '';
           alert('clear');
       $.ajax({
    
    
              type: "POST",
               url:  window.location.pathname + "Account/Register",
    
               success: function (response) {
                   $.ajax({
                       url: window.location.pathname + '/Account/Logon',
                       type: 'GET',
                   })
            .success(function (result) {
                alert("user saved successfully");
    
                // window.location ="";
            });
               },
               error: function (xhr, status, error) {
                   alert("Failed");
               }
           });
    }
    

    I'm getting a Failed alert. Please help

  • gs11111
    gs11111 over 10 years
    My problem is I don use ajax request in my controller.
  • Arun Manjhi
    Arun Manjhi over 10 years
    Just Replace the " url: window.location.pathname + '/Account/Logon' " into " url: '@Url.Action( "Register", "Account")' "