Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework WEB API中如何将多个模型作为参数传递_Entity Framework_Asp.net Web Api_Model View Controller_Postman_Webapi - Fatal编程技术网

Entity framework WEB API中如何将多个模型作为参数传递

Entity framework WEB API中如何将多个模型作为参数传递,entity-framework,asp.net-web-api,model-view-controller,postman,webapi,Entity Framework,Asp.net Web Api,Model View Controller,Postman,Webapi,有人能帮我在WEB API中将多个模型作为参数传递给请求的内容吗 我有两个不同的模范学生和员工 public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public string Branch { get; set; } } public class Employee {

有人能帮我在WEB API中将多个模型作为参数传递给请求的内容吗

我有两个不同的模范学生和员工

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; }
    }
我已经创建了一个API,希望将这两个模型作为参数传递给我的操作方法InsertTestAPI

[HttpPost]
[Route("TestAPI")]

public HttpResponseMessage InsertTestAPI(Student modelStudent, Employee modelEmployee)
{
    // other logical operations
}
当我在请求体中将这些模型作为JSON传递时,我从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()"
}
{
“$id”:“1”,
“消息”:“发生错误。”,
“ExceptionMessage”:“无法将多个参数('modelStudent'和'modelEmployee')绑定到请求的内容。”,
“异常类型”:“System.InvalidOperationException”,
“StackTrace”:“在System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext,CancellationToken CancellationToken)\r\n在System.Web.Http.Controllers.ActionFilterResult.d_u5.MoveNext()\r\n---从引发异常的上一个位置开始的堆栈结束跟踪----\r\n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n在System.Web.Http.Dispatcher.HttpControllerDispatcher.d_15.MoveNext()
}

有人能帮我吗?

之后你可以在学生课上调用Empolyee类。然后你可以在api中调用多个模型调用

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

之后可以在学生类中调用Empolyee类,然后可以在api中调用多个模型调用

public class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public string Branch { get; set; }
        public Employee EmpData {get;set;}
    }
以这种方式创建StudentEmpendedModel类

public HttpResponseMessage InsertTestAPI(StudentEmployeModel model)
{// other logical operations }
这样要求

    { "students":   { "studentId":"0", "studentName":"Mehmet", "branch":"software" } ,
 "employees":
 {"employeeId ":0,"employeeName":"Test","department":"IT"} }
通过这种方式,您可以将请求作为Json发送

以这种方式创建StudentEmpendedModel类

public HttpResponseMessage InsertTestAPI(StudentEmployeModel model)
{// other logical operations }
这样要求

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

通过这种方式,您可以将请求作为Json发送

Hi-Mehmet,感谢您宝贵的回复,您能告诉我如何在学生和员工模型中设置值,同时我将StudentEmployeModel作为Json发送到请求正文中吗?我在评论中写下了答案Hi-Mehmet,感谢您宝贵的回复,当我在请求正文中以JSON形式传递StudentEmployeModel时,你能告诉我如何在学生和员工模型中设置值吗?我在评论中写下了答案谢谢你,Divyanshi感谢你宝贵的答案,你的解决方案也在起作用,但我可以接受所有人的答案,所以我也对你的答案进行了投票。谢谢你。@NikunjSatasiya谢谢你,Divyanshi,谢谢你宝贵的回答,你的解决方案也有效,但我可以接受所有人的答案,所以我也对你的答案投了赞成票。谢谢。@NikunjSatasiya谢谢你