Nikunj Satasiya

Nikunj Satasiya

  • 276
  • 6.1k
  • 3.5m

How to pass multiple model as parameter in WEB API

Jan 28 2021 5:14 AM
Can anyone please help me to pass multiple models as a parameter to the request's content in WEB API?
 
I have 2 different Model Student and Employee 
  1. public class Student  
  2.     {  
  3.         public int StudentId { getset; }  
  4.         public string StudentName { getset; }  
  5.         public string Branch { getset; }  
  6.     }  
  7.   
  8. public class Employee  
  9.     {  
  10.         public int EmployeeId { getset; }  
  11.         public string EmployeeName { getset; }  
  12.         public string Department { getset; }  
  13.     }  
I have created an API and want to pass both these models as parameters in my action method InsertTestAPI.
  1. [HttpPost]  
  2. [Route("TestAPI")]  
  3.   
  4. public HttpResponseMessage InsertTestAPI(Student modelStudent, Employee modelEmployee)  
  5. {  
  6.     // other logical operations  
  7. }  
When I pass these models as JSON in the request body, I get the following error in Postman.
  1. {  
  2.     "$id""1",  
  3.     "Message""An error has occurred.",  
  4.     "ExceptionMessage""Can't bind multiple parameters ('modelStudent' and 'modelEmployee') to the request's content.",  
  5.     "ExceptionType""System.InvalidOperationException",  
  6.     "StackTrace""   at System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"  
  7. }  
Can anyone please help me?

Answers (5)