简体   繁体   English

使用Ajax POST查询MVC控制器,然后使用模型重定向

[英]Query MVC controller with Ajax POST then redirect with a model

Let's say that I have data that has a complicated format (a list of object) that I want to post to an MVC controller, and then I want this controller to redirect me to the page i want with the model it generated. 假设我有要发送到MVC控制器的具有复杂格式(对象列表)的数据,然后希望该控制器将其重定向到我要使用生成的模型的页面。 Just like a POST request from a regular form would do. 就像来自常规表单的POST请求一样。

Instead, when I do this angular request with angular: 相反,当我用angular执行此角度请求时:

$http({
            method: 'POST',
            url: '/user/search',
            data: $scope.dataToSearch
        }).success(function() {
                // what do put here ?
        })
        .error(function() {

        });
  • the controller successfully receives the data 控制器成功接收数据
  • the controller successfully generates the model I want 控制器成功生成我想要的模型
  • the controller returns "View("viewWanted", model") but it DOESN'T redirect. 控制器返回“ View(“ viewWanted”,model“),但不会重定向。

All I receive is a status code 200, and the raw html of the page I wanted without redirecting me to this actual page. 我所收到的只是一个状态码200,以及我想要的页面的原始html,而无需将我重定向到此实际页面。 The thing is, if I do "window.location" on success, my controller won't generate the model because I have to post the data. 问题是,如果成功执行“ window.location”,我的控制器将不会生成模型,因为我必须发布数据。

In conclusion, how to post then redirect like with a regular form, but with using Ajax/Angular/Javascript in general ? 总之,如何像常规形式那样发布然后重定向,但是通常使用Ajax / Angular / Javascript? Thanks. 谢谢。

You may use the Angular promises for return and wait the success request. 您可以使用Angular promises进行退货并等待成功请求。 So, you need more control from controllers, Restangular is better for this. 因此,您需要更多来自控制器的控制, Restangular对此更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM