2014-11-08 2 views
1

Я пытаюсь отправить форму из контроллера angularjs в виде $ http.post() в asp.net web api method. Но он отправляет нулевое значение. Вот мой код

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

Показать 'console.log ($ scope.newpost);' имя свойства должно соответствовать, и перед POST сделать 'JSON.stringify' – Max

ответ

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"); 
    }); 
+0

Это не работает. –

+0

, пожалуйста, опубликуйте данные $ scope.newpost. –

+0

См. Обновление. –

Смежные вопросы