0

I have two class and basically I am trying to call two separate models with two store procedures to insert data in both table. I wrote my controller like this:

[HttpPost]
public IHttpActionResult Adddata([FromBody]IList<Details> details,[FromBody] Request request)
{
    var changerequestResponse = db.sp_AddChangerequests(request.EmpID, request.CreatedDate, request.Status).SingleOrDefault();
    GenericResponse objResponse = null;
    foreach (Details detail in detailss)
    {
        var data = JsonConvert.SerializeObject(db.sp_AddChangerequestDetails(detailss.CRID, detailss.Category, detailss.Info, detailss.Col, detailss.Reason).SingleOrDefault(), Formatting.Indented, settings);
        objResponse = JsonConvert.DeserializeObject<GenericResponse>(data);

    }
    return Ok(objResponse);

}

and my controller js file like this:

$scope.SubmitrequestForm = function(){            
    console.log($scope.changereqcol);
    var req = {
        method: 'POST',
        url: WebApiUrl + 'Adddata',
        headers: {
            'Content-Type': 'application/json'
        },
        data: JSON.stringify($scope.changereqcol)
    }
    console.log()
    $http(req).then(function (data, status) {
        console.log(data);
    }, function (err, status) {
        console.log(err);
    });
};

when I am trying to submit the data I am getting above error. Can you please tell me what I am missing

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Raj
  • 9
  • 6
  • What error are you getting? Also, there can be at most one parameter per action decorated with `[FromBody]`. Therefore, I suggest you to merge your models in one request; – Rahul Sharma Dec 24 '19 at 06:26
  • Can't bind multiple parameters to the request content. – Raj Dec 24 '19 at 06:28
  • I tried with that already but getting compile time error.. Maybe I am missing something as I am very new in this so if you could give me any example just for better understanding – Raj Dec 24 '19 at 06:40
  • Please take a look at this question: https://stackoverflow.com/questions/44599041/mvc-web-api-error-cant-bind-multiple-parameters – Rahul Sharma Dec 24 '19 at 06:44
  • Did you get this working? – Rahul Sharma Dec 27 '19 at 06:40
  • No.. I think I am missing something.. I am very new in this so not getting exactly what I need to do.. Can you please correct my code. – Raj Dec 27 '19 at 12:34
  • Can you show us your complete `Controller` and `View` code ? – Rahul Sharma Dec 27 '19 at 12:45
  • Sorry i can't write whole code here because of some restrictions and whole controller has so many lines of code which is not possible to mention here.. But i gave the respective post method and api call code above. Is it ok for you?? – Raj Dec 27 '19 at 12:59
  • What is the Model type for `$scope.changereqcol data?` Also remove the extra `[FromBody]`: `public IHttpActionResult Adddata([FromBody]IList
    details,Request request)`
    – Rahul Sharma Dec 27 '19 at 13:13
  • IList
    details
    – Raj Dec 29 '19 at 02:40
  • Did you try my above comment ? – Rahul Sharma Dec 29 '19 at 08:46
  • yes removed the extra [FromBody] – Raj Dec 30 '19 at 16:41

2 Answers2

2

From this source:

Don't apply [FromBody] to more than one parameter per action method. Once the request stream is read by an input formatter, it's no longer available to be read again for binding other [FromBody] parameters.

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
  • This is the actual answer. You can't bind FromBody more than once. Make FromBody a class that contains all the data you need – Zakk Diaz Mar 03 '20 at 20:55
0

Please Take look at jquery POST Method in keys : value set your data

function verifyRiderAppID() {

 $.post("/url ", {
   Parms: value
  },
  function (data, status) {
   if (status === 'success') {

   } else {
    //else logic
   }
  });
}