1

I am trying to call one method but parameter is coming out as null

Server Side Code

[HttpPost]
        public IHttpActionResult PostRule(ActionRuleParameter actionRule)
        {
            // SOME CODE               
            return BadRequest();
        }

        public class ActionRuleParameter
        {
            public string action;
            public string rule;
        }

Client Side Code

addRule: function ($scope) {
            //var data = { "action": "post", "rule": { "ID": "1", "Name": "Ramesh", "PassFail": "Pass" } }

            var data1 = { "action": "post","rule":"rule" };


            $http({
                url: urlContent + '/api/Rules',
                method: "POST",
                data: { "actionRule": data1 }
            }).success(function (response) {
                $scope.rules = response;
            });;
        },

action and rule are coming out as null.

2 Answers 2

1

You need to add [FromBody] before your parameter

    public IHttpActionResult PostRule([FromBody]ActionRuleParameter actionRule)
    {
        // SOME CODE               
        return BadRequest();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I ran in to this issue with Angular and the new ASP.NET stack. Adding [FromBody] fixed it for me so try:

 [HttpPost]
 public IHttpActionResult PostRule([FromBody]ActionRuleParameter actionRule)
 {
     // SOME CODE               
     return BadRequest();
 }

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.