AngularJS $ http.post()向ASP.NET Web API控制器方法发送null

时间:2014-11-08 10:28:40

标签: c# angularjs asp.net-web-api

我试图将angularjs控制器中的表单作为$ http.post()提交到asp.net web api方法。但它发送空值。这是我的代码

//angular controller
$scope.newpost = {};   
$scope.save = function () {
    console.log($scope.newpost); // it logs data accurately
    $http.post('/api/NewPost', $scope.newpost).
      success(function (data, status, headers, config) {
          alert("ok");
      });
}


//api
public HttpResponseMessage post(NewPost newpost) //fields are null here. I tried [FromBody] $ [FromURI], but no luck
{
     posts.Add(newpost);
     String id = newpost.Id; //saving is ok.
     return Request.CreateResponse(HttpStatusCode.OK);
}


//model
public class NewPost
{
     public String Title { get; set; }
     public String Content { get; set; }
     public String Tags { get; set; }
}

console.log($scope.newpost) displays-
Object {Title: "t", Content: "tt", Tags: "ttt"}

任何帮助?

1 个答案:

答案 0 :(得分:0)

使用它:

console.log($scope.newpost.Title ); // it should logs data accurately
console.log($scope.newpost.Content );// it should logs data accurately
console.log($scope.newpost.Tags  );// it should logs data accurately
$http.post('/api/NewPost', $scope.newpost).
  success(function (data, status, headers, config) {
      alert("ok");
  });