从角柱发送数据到MVC

时间:2014-03-19 09:56:33

标签: asp.net-mvc angularjs asp.net-mvc-4

我们正在使用angularjs将表单发布到MVC控制器进行处理

$scope.resultsFilter = function () {
    $scope.data = [];
    $scope.data = {
        startdate: $scope.$$childHead.resultfilter.startdate,
        enddate: $scope.$$childHead.resultfilter.enddate,
        frequency: $scope.resultfilter.frequency,
        direction: $scope.resultfilter.direction
    };

    var url = '/user/GetResults/';
    $http.post(url, $scope.data).success(function (data, status, headers, config) {
                     if (data.msg != '') {
                         $scope.msgs.push(data.msg);
                     }
                     else {
                         $scope.errors.push(data.error);
                     }
                 }).error(function (data, status) { // called asynchronously if an error occurs
                     // or server returns response with an error status.
                     $scope.errors.push(status);
                 });

};
});

并且MVC控制器是: -

 [HttpPost]
    public ActionResult GetResults()
    {

    }

我们如何访问表单发布的数据?

2 个答案:

答案 0 :(得分:3)

如果您的POCO等效于该数据,请执行以下操作:

public class MyData{
  public DateTime startdate{get;set;}
  public DateTime enddate{get;set;}
  ....
}

您可以尝试:

[HttpPost]
public ActionResult GetResults(MyData myData)
{

}

使用以下HTTP Post:

$http({
     url: '/controllerName/GetResults/',
     method: "POST",
     data:$scope.data,
   })
   .success(function (data, status, headers, config) {

   }).error(function (data, status, headers, config) {

});

答案 1 :(得分:0)

  

您可以使用模型对象获取MVC模型并将其作为json传递给函数   数据

click=someFunc('@Json.Encode(Model)');