$ http.post导致Get错误

时间:2014-08-31 11:46:35

标签: angularjs forms http

我试图通过调用$ http.post来执行下面html发生的相同操作。使用以下HTML时,将成功调用post方法。

<form action="/register/" enctype="multipart/form-data" method="post" class="ng-pristine ng-valid" _lpchecked="1">

HTML替换为:

<form name="registrationForm">

并且angularJS submit方法由以下内容调用:

    $scope.save = function () {
    $http.post('/register/', {model:$scope.registration})
    .success(function (data) {
    })
    .error(function (err) {
        $log.error(err);
    });
};

使用$ log.error记录的错误是:

A public action method 'Get' was not found on controller 'RegistrationSurfaceController'.

我的Post方法永远不会被调用。

    [HttpGet]
    public ActionResult Get()
    {
        var model = new RegistrationModel();
        return PartialView("umbRegistrationForm", model);
    }

    [HttpPost]
    public ActionResult Post(RegistrationModel model)
    {    
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        model.Id = Guid.NewGuid();
        model.Amount = 20;

        var googleDocs = new GoogleDocsProcessor();
        googleDocs.AddRegistration(model, DateTime.Now);

        TempData.Add("RegistrationSuccess", true);
        TempData.Add("RegistrationModel", model);
        return View(model);
    }

这是关于开发工具的网络标签。

networktab

1 个答案:

答案 0 :(得分:0)

替换以下代码:

$scope.save = function () {
    $http.post('/register/', $scope.registration)
    .success(function (data) {
    })
    .error(function (err) {
        $log.error(err);
    });
};

代码:

$scope.save = function () {
    $http.post('/register/', {model:$scope.registration})
    .success(function (data) {
    })
    .error(function (err) {
        $log.error(err);
    });
};

您将$scope.registration传递给后端的model变量。所以你必须在api call中提及它。我认为这会对你有帮助:)。