如何从Javascript传递到WEB API控制器HttpPOST复合阵列? [英] How to pass a composite array from Javascript to WEB API controller HttpPOST?

查看:156
本文介绍了如何从Javascript传递到WEB API控制器HttpPOST复合阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的的Javascript code

//insert the employee and department record
    this.insertEmployeeDepartment = function (Employee) {       

        var request = $http({
            method: "post",
            url: "/Employee/InsertEmployeeDepartment",
            contentType: "application/json",
            data: JSON.stringify(Employee)
        });
        return request;
    }

EmployeesAPIController.cs

using System.Web.Http;

namespace EmployeeService
{
    [RoutePrefix("Employee")]
    public class EmployeesAPIController : ApiController
    {  
        [HttpPost]
        [Route("InsertEmployeeDepartment")]
        public EmployeeDepartment InsertEmployeeAndDepartment([FromBody]EmployeeDepartment emp)
        {
            var xx = emp;           
        }

    }
}

EmployeeDepartment.cs

using System.Collections.Generic;

namespace Test
{
    public class EmployeeDepartment
    {
        public IEnumerable<Employee> Employees { get; set; }
        public IEnumerable<Department> Departments { get; set; }    
    }
}

型号 -

Employee.cs

public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }        
        public int Age { get; set; }
        public int Salary { get; set; }
        public int DepartmentId { get; set; }
    }

Department.cs

public class Department
    {
        public int Deptid { get; set; }
        public string Deptname { get; set; }
    }

这是我从Javascript传递数组是在

The array that I am passing from Javascript is as under

在控制器的方法,该值作为即将到来的无效

In the controller method, the value is coming as null?

什么错,我在做什么?

推荐答案

鉴于你的JavaScript对象数组(不知道这是只限于两个条目),我们可以重新写你的JavaScript后的模型来模仿你的WebAPI的要求模型。

Given your Javascript Object array (and not sure if it is limited to just two entries), we can re-write your javascript post model to mimic your WebApi request model.

喜欢的东西(remeber限于2个对象的JavaScript数组中)。

Something like (remeber limited to 2 objects in the javascript array).

this.insertEmployeeDepartment = function (Employee) {
    //construct a new object to match the WebApi Object
    var dto = {
        Employees: [Employee[0]], //Employee[0] is the employee record
        Departments: [Employee[1]] //Employee[1] is the department record
    };

    var request = $http({
        method: "post",
        url: "/Employee/InsertEmployeeDepartment",
        contentType: "application/json",
        data: JSON.stringify(dto)
    });
    return request;
}

现在,如果你的JavaScript数组每方法构造不同,你将不得不格式化您的新模型数据对象不同。

Now if your JavaScript array is constructed differently per method you will have to format your new model data object differently.

要使它完全匹配,因为我看到你的WebAPI 的DepartmentID 属性是不是在员工[0] 记录我们可以通过手动复制它。如

To make it match exactly as i see your WebApi DepartmentId property is not on the Employee[0] record we can copy it over manually. Such as.

this.insertEmployeeDepartment = function (Employee) {
    //construct a new object to match the WebApi Object

    Employee[0]['DepartmentId'] = Employee[1].Deptid;

    var dto = {
        Employees: [Employee[0]], //Employee[0] is the employee record
        Departments: [Employee[1]] //Employee[1] is the department record
    };

    var request = $http({
        method: "post",
        url: "/Employee/InsertEmployeeDepartment",
        contentType: "application/json",
        data: JSON.stringify(dto)
    });
    return request;
}

这篇关于如何从Javascript传递到WEB API控制器HttpPOST复合阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆