asp.net-mvc - 在mvc中使用ajax调用重定向到另一个 View

标签 asp.net-mvc asp.net-mvc-3 jquery form-submit

我有一个注册表单,注册成功后,它会重定向到另一个 View 。 我使用提交按钮将数据插入数据库,我想清除当前 View 中的值并将用户重定向到同一 Controller 下另一个 View 中的登录页面。 提交类型没有执行 jquery 中提到的操作。请帮助我

这是我的代码预览

帐户/注册

帐户/登录

 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");
                     });

更新:

   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");
           }
       });
}

我收到失败警报。 请帮忙

最佳答案

如果您想在成功登录时重定向页面,请在成功时编写重定向代码(ajax 成功)。 经验:

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

         // window.location ="";
          });

关于asp.net-mvc - 在mvc中使用ajax调用重定向到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18097352/

相关文章:

asp.net-mvc - 属性如何阻止 MVC 操作的执行?

asp.net-mvc-3 - Html.ActionLink 魔法

asp.net - ASP.NET MVC问题与表单例份验证部分的配置

jQuery 在 Chrome 和 Mac OS 上停止运行

javascript - jQuery 导航栏鼠标悬停问题

javascript - 隐藏输入字段中的某个字符 jQuery

c# - ASP.NET MVC 项目 web.config 和 app.config 文件

asp.net-mvc - 模型和 View 模型之间的区别

asp.net - 如何在 .asp 页面打开时调用 global.asax

asp.net-mvc - 如何使用 Entity Framework 和成员资格表初始化数据库