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?

4 Answers 4

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

Use,

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

});

Comments

0

Try this

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

    });

Comments

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 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.