Ian
Ian

Reputation: 291

Angularjs $http.post but asp.net 5 controller gets null

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

Answers (4)

Nag
Nag

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

adem caglin
adem caglin

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

Hassan Tariq
Hassan Tariq

Reputation: 740

Try this

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

    });

Upvotes: 0

Low Flying Pelican
Low Flying Pelican

Reputation: 6054

Use,

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

});

Upvotes: 0

Related Questions