MVC动作控制器未呈现页面

时间:2019-01-07 17:07:44

标签: c# asp.net-mvc

在编译网站(Registration.cshtml)上所需的表单后,表单数据将成功发送到sqlite db中。从那回来,我想被发送到Main.cshtml网页。问题在于,假定要呈现Main.cshtml视图的Action控制器没有执行该操作。

我已经尝试过多次更改Redirect / RedirectToAction和View方法,但这是失败的。

   return View("Main");
   return RedirectToAction("Main");
   return JavaScript("window.location = window.location.origin + /Pages/Main");

PagesController.cs

   [HttpPost]
   public ActionResult Register {
        if (insert data in db == true) {
            return RedirectToAction("Main");
        }

        return View();
   }

   public ActionResult Main(){
        return View();
   }

Main.cshtml

    @{
         Layout = "~/Views/Layouts/LoggedMasterPage.cshtml";
         ViewBag.Title = "Benvenuto in ETL365"; 
     }
     @section Head{
          <link rel="stylesheet" href="~/Content/Main.css" />
          <script type="text/javascript" src="~/Scripts/Main.js"></script>
     }

我的Register.js的一部分

         $("#register").dxButton({
             text: "Registrati",
             type: "normal",
             hint: "Clicca per registrarti",
             onClick: function () {
               if($('#password').
               dxValidator('instance').validate().isValid &&
               $('#email').dxValidator('instance').validate().isValid) {
                     let email = $("#email").dxTextBox('instance').option('value');
                     let pass = $("#password").dxTextBox('instance').option('value');

                     $.ajax({
                         url: window.location.origin + '/Pages/Register',
                         type: "POST",
                         data: '{"email":"' + email + '","password":"' + pass + '"}',
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",
                      });
               }
             }
          });

我已经在Main.cshtml上使用断点了,它实际上是在

之后到达的
     return View();

通过操作Main()完成。 我期望被重定向到mylocalhost:xxxx / Pages / Main 我得到的是绝对没有,我一直在mylocalHost:xxxx / Pages / Register

2 个答案:

答案 0 :(得分:2)

成功调用ajax之后,您需要重定向到您的主页。

$.ajax({
...
    success:
    {
    location.href = 'mylocalhost:xxxx/Pages/Main'
    }
...

答案 1 :(得分:1)

您不能将RedirectToAction用于AJAX呼叫响应,因为AJAX请求旨在更新视图而无需重新加载整个页面。您需要像这样将URL作为JSON响应传递:

[HttpPost]
public ActionResult Register(string email, string password) 
{
    if (condition) // put your condition here
    {
        return Json(new { 
            status = "Success", 
            url = Url.Action("Main", "Pages")
        });
    }

    // since this controller receives AJAX request, the partial view should be used instead
    return PartialView("_Register");
}

然后在AJAX success条件下,您可以检查响应状态并重定向到目标URL:

$.ajax({
    url: '@Url.Action("Register", "Pages")',
    type: "POST",
    data: { email: email, password: pass },
    success: function (result) {
        if (typeof result.status !== undefined && result.status == "Success") {
            location.href = result.url; // redirect to main page
        }
        else {
            $('#register').html(result); // refresh partial view
        }
    }
});

相关问题:

return url.action as json object mvc