Pawan
Pawan

Reputation: 43

webapi invoked from Angular, but parameters are always received null

I am trying to create a webApi method that accepts complex type, this method is invokable via AngularJs $http.post() but the values received are always received null. Tried almost all possible suggestions available on internet, nothing seems to work!! My code snippet below.

var config = {
        method: "POST",
        url: URLService.myBaseURL() + 'api/RequestApi/createNewRequest',
        headers: {
            "Content-Type": "application/json"
        },
        data: $scope.dataToSubmit
    }

    $http(config)
    .then(function (success) {
        alert('A New request Created Successfully!!')
    }, function (error) {
        alert('There was an error while creating the request!!')
    })

C# webApi code

public class RequestApiController : ApiController
{
    [HttpPost]
    public string createNewRequest([FromBody]Request dataSubmitted)
    {
      //BL here...
    }
}

public class Request {
  List<Activities> clearanceReqDetails { get; set; }
  .....
}
public class Activities{
  ....
}

Any help!!

Upvotes: 0

Views: 113

Answers (1)

Pawan
Pawan

Reputation: 43

Issue resolved!! It was an issue with the access specifiers of the class properties, for serialization/deserialization to happen not only the class, but also class properties need to be have proper access specifiers. In my case, I resolved the issue by adding 'public' access specifier to properties of both the classes as below.

public class Request {
  **public** List<Activities> clearanceReqDetails { get; set; }
...
}
public class Activities{
  **public** ...
}

Upvotes: 0

Related Questions