1

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?

Ian
  • 291
  • 1
  • 3
  • 9

4 Answers4

1

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)
{

}
adem caglin
  • 22,700
  • 10
  • 58
  • 78
0

Use,

$http.post("Home/PostData", { params: {name:'Peter',age:18}}).success(function (response) {

});
Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
0

Try this

 $http.post("Home/PostData",{ name: "Peter", age:18}).success(function (response){

    });
Hassan Tariq
  • 730
  • 7
  • 15
0

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 }

Nag
  • 1
  • 3