0

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

public class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public string Branch { get; set; }
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string Department { get; set; }
    }

I have created an API and want to pass both these models as parameters in my action method InsertTestAPI.

[HttpPost]
[Route("TestAPI")]

public HttpResponseMessage InsertTestAPI(Student modelStudent, Employee modelEmployee)
{
    // other logical operations
}

When I pass these models as JSON in the request body, I get the following error from Postman.

{
    "$id": "1",
    "Message": "An error has occurred.",
    "ExceptionMessage": "Can't bind multiple parameters ('modelStudent' and 'modelEmployee') to the request's content.",
    "ExceptionType": "System.InvalidOperationException",
    "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()"
}

Can anyone please help me?

2 Answers 2

1
public class StudentEmployeModel
{
 public Student Students{get;set;}


public Employee Employees{get;set;}
}

Create the StudentEmpendedModel class this way.

public HttpResponseMessage InsertTestAPI(StudentEmployeModel model)
{// other logical operations }

request this way

    { "students":   { "studentId":"0", "studentName":"Mehmet", "branch":"software" } ,
 "employees":
 {"employeeId ":0,"employeeName":"Test","department":"IT"} }

This way you can send requests as Json

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Mehmet, Thank you for your valuable response, can you please let me know how can I set value in the Students and Employees model while I pass StudentEmployeModel as JSON in the request body?
I wrote the answer in the comment
1

You can call Empolyee class in student class after that .then you can call multiple model call in api

public class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public string Branch { get; set; }
        public Employee EmpData {get;set;}
    }

1 Comment

Thank you, Divyanshi for your valuable answer, your solution is also working but I can accept the answer for all so I upvoted your answer also. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.