Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an angularjs resource as seen below:

  return $resource('/api/report',
     { },
     {
       save: { method: 'POST', url: '/api/fileupload/savefile', isArray: false },
     });

My angularjs controller that uses this resource is below:

$scope.save = function () {
            var reportFieldsSample = [{ Field: 'ff', Value: 'gg' }, { Field: 'aa', Value: 'dd' }];
            Report.save({ reportParams: reportFieldsSample},
                { },
                function (content) {
                    console.log(content);
                });

My WebApiConfig for this route is:

   config.Routes.MapHttpRoute(
        "SaveFile",
        "api/fileupload/savefile",
        new { controller = "FileUpload", action = "SaveFile" },
        new { httpMethod = new HttpMethodConstraint("POST") });

My mvc api controller that receives this is below:

[HttpPost]
public HttpResponseMessage SaveFile(IList<Parameter> reportParams)
    {
        //reportParams is null
    }

Parameter class is declared in this way:

public class Parameter
{
    public string Field { get; set; }
    public string Value { get; set; }
}

The request enters my api controller but the reportParams argument of the controller is always null. Can you help me point out the problem? Thanks

share|improve this question
 
Can you try to send data as Report.save(reportFieldsSample,...) and see if it works –  Chandermani Dec 1 at 5:27
 
it has an error: TypeError: Object doesn't support property or method 'push' –  eaon21 Dec 1 at 5:37
 
Client side or server side error? –  Chandermani Dec 1 at 5:40
 
client side.. i found the error in the browser's console log –  eaon21 Dec 1 at 5:40
 
TypeError: Object doesn't support property or method 'push'; at copy (localhost:54429/Scripts/angular.js:826:9); at Resource (localhost:54429/Scripts/angular-resource.js:433:9) –  eaon21 Dec 1 at 5:43
show 3 more comments

1 Answer

up vote 0 down vote accepted

The reason that you are getting null in you web api controller is because the structure you are sending from client is not matching the server parameter declaration.

What you can do on the server side is to declare object such as

class ReportParamsModel {
  public IList<Parameters> reportParams {get; set;}
}

You controller method becomes

public HttpResponseMessage SaveFile(ReportParamsModel params)

and you are set to go

You can also use the dynamic support in webapi, where the controller method becomes

public HttpResponseMessage SaveFile(dynamic data) {
    //data.reportParams contains you array (but each object again is a dynamic object.
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.