Skip to content Skip to sidebar Skip to footer

"can't Bind Multiple Parameter To The Request's Content." In Web Api And Angularjs

When Multiple parameters pass in WebApi it results as an exception 'Can't bind multiple parameter to the request's content.'.Have any solution for following code public class A1 {

Solution 1:

You might want to use a model which contains your data:

publicclassA1
{
    publicint id { get; set; }
    publicstring name { get; set; }
}

publicclassA2
{
    publicint id2 { get; set; }
    publicstring name2 { get; set; }
}

publicclassAModel 
{
    public A1 Emp { get; set; }
    public A2 EmpMarks { get; set; }
}


[Route("Save")]
[HttpPost]
publicstringSave(AModel aData)
{
    // ... your logic here
}

Solution 2:

The issue occours because you are declaring the [FromBody] attribute twice. As per design, http POST does only have one body and the [FromBody] will try to read all content in the body and parse it to your specified object.

To solve this issue you need to create an object which matches your client object which is being attach to the request body.

publicclassRequestModel
{
    publicA1Emp {get;set;}
    publicList<A2> EmpMarks {get;set;}
}

Then fetch that from the request body in your post method

[Route("Save")]
[HttpPost]
publicstringSave([FromBody]RequestModel Emps)

Solution 3:

[Route("Save")]
[HttpPost]
publicstringSave(JObject EmpData)
{
dynamic json = EmpData;
A1 Emp=json.Emp.ToObject<A1>();
List<A2> EmpMarks=json.ToObject<List<A2>>();
}

It is an another option.It is work for me

Solution 4:

If you want to pass multiple parameters to a POST call, you can simply do as below and add as many to match the service.

var data = new FormData();
data.append('Emp', $scope.Emp);
data.append('EmpMarks', $scope.EmpMarks);
$http.post('/api/Employee/Save', **data**, { 
  withCredentials : false,
  transformRequest : angular.identity,
  headers : {
            'Content-Type' : undefined
    }
  }).success(function(resp) { });

Post a Comment for ""can't Bind Multiple Parameter To The Request's Content." In Web Api And Angularjs"