0

I am trying to post a simple JSON to C# Controller but I am getting null. I am new to AngularJS.

here is the angularjs code that posts

k = "{ 'id': 1,'name': 'A green door','price': 12.50}";

$http.post("/Inventory/PostSO", k)
    .then(function (response) {
        $scope.message = response.status;
    },
    function (error, status) {
        $scope.message = error.error;
    });

this is the C# code but string data is always null

public ContentResult PostSO(string data)
{
    return null;
}

What am I doing wrong? Thanks

2
  • Why do you send a object as string? Commented Oct 4, 2017 at 10:26
  • 1
    Did you solve your problem? Commented Oct 28, 2017 at 23:43

2 Answers 2

1

First of all set the request headers.

Content-Type - application/json

At second controller action parameter is string, it must be a model, because the action descriptor tries to map sended model to C# object, create a simple class, and define all properties like data in your json example.

And try again :)

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

Comments

1

Add FromBody-Attribute to Action-Parameter:

public ContentResult PostSO([FromBody]string data)
{
    return null;
}

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.