Reputation: 291
In my angularjs code I have this:
$http.post("Home/PostData", {name:'Peter',age:18}).success(function (response) {
});
And I have this method in my asp.net 5 controller:
[HttpPost]
public IActionResult PostData(string name,int age)
{
}
I've put a break point on the method and it gets fired, but my name is null and age is 0.
I have done that before by using asp.net mvc 5 and that worked.
Why is wrong in asp.net 5?
Upvotes: 1
Views: 1116
Reputation: 1
In my case controller parameter name is same name as property in the model. Its failing because of that reason.
For Ex- in controller public JsonResult Method(BindingModel firstname, ViewState viewState)
in BindingModel--
class BindingModel{ string firstname, string lastname }
Upvotes: 0
Reputation: 24083
Change your controller code like below:
public class PostModel
{
public string name { get; set; }
public int age { get; set; }
}
[HttpPost]
public IActionResult PostData([FromBody]PostModel model)
{
}
Upvotes: 1
Reputation: 740
Try this
$http.post("Home/PostData",{ name: "Peter", age:18}).success(function (response){
});
Upvotes: 0
Reputation: 6054
Use,
$http.post("Home/PostData", { params: {name:'Peter',age:18}}).success(function (response) {
});
Upvotes: 0