Please help me out.
I want to pass string to Post method of WebApi controller using $http method.But I am getting null string at controller side.
Here is my client side code.
$http({
url: 'http://localhost/MyWebApi/api/home',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
data: JSON.stringify({name: $scope.newProduct})
}).success(function (data, status, headers, config) {
$scope.products = data;
$scope.newProduct = "";
console.log(data);
})
.error(function (data, status, headers, config) {
console.log(data);
});
And here is my WebApi controller
[HttpPost]
public IHttpActionResult Post([FromBody]string newProduct)
{
var db = new MVCEntities();
db.Products.Add(new Product() { ProductName = newProduct });
db.SaveChanges();
var products = db.Products.ToList();
return Ok();
}
Thanks