最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Can't bind multiple parameter to the request's content." in web api and angularJ

programmeradmin1浏览0评论

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
{
   public int id {get;set;}
   public string name {get;set;}
}

public class A2
{
   public int id2 {get;set;}
   public string name2 {get;set;}
}

[Route("Save")]
[HttpPost]
public string Save([FromBody]A1 Emp, [FromBody]List<A2> EmpMarks)
{
}

JS file

$http({
    method: "post",
    url: "/api/Employee/Save",
    data: JSON.stringify({
        Emp: $scope.Emp,
        EmpMarks: $scope.EmpMarks
    })
}).then(function (response) {

}, function () {
    alert("Error Occur");
})

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
{
   public int id {get;set;}
   public string name {get;set;}
}

public class A2
{
   public int id2 {get;set;}
   public string name2 {get;set;}
}

[Route("Save")]
[HttpPost]
public string Save([FromBody]A1 Emp, [FromBody]List<A2> EmpMarks)
{
}

JS file

$http({
    method: "post",
    url: "/api/Employee/Save",
    data: JSON.stringify({
        Emp: $scope.Emp,
        EmpMarks: $scope.EmpMarks
    })
}).then(function (response) {

}, function () {
    alert("Error Occur");
})
Share Improve this question edited Jun 22, 2017 at 6:18 Mehdi Dehghani 11.6k6 gold badges63 silver badges66 bronze badges asked Jun 22, 2017 at 5:34 safeena rasaksafeena rasak 4042 gold badges6 silver badges18 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 6

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

public class A1
{
    public int id { get; set; }
    public string name { get; set; }
}

public class A2
{
    public int id2 { get; set; }
    public string name2 { get; set; }
}

public class AModel 
{
    public A1 Emp { get; set; }
    public A2 EmpMarks { get; set; }
}


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

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.

public class RequestModel
{
    public A1 Emp {get;set;}
    public List<A2> EmpMarks {get;set;}
}

Then fetch that from the request body in your post method

[Route("Save")]
[HttpPost]
public string Save([FromBody]RequestModel Emps)
[Route("Save")]
[HttpPost]
public string Save(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

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) { });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论