1

I'm trying to submit a form from angularjs controller as $http.post() to asp.net web api method. But it sends null value. Here is my code

//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"}

Any help?

1
  • 1
    Show the console.log($scope.newpost); the name of property must match, also before POST do JSON.stringify Commented Nov 8, 2014 at 10:38

1 Answer 1

0

use this:

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");
  });
2
  • please post $scope.newpost data. Commented Nov 8, 2014 at 10:53
  • I found the problem. It comes from MongoRepository. NewPost model was inherited from MongoRepository.Entity. I removed it and the problems is solved. Commented Nov 8, 2014 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.