【发布时间】:2014-03-31 19:22:12
【问题描述】:
我正在开发一个 Angular/Web API 应用程序。这是一个我让自己在家忙碌的项目,以便我可以熟悉新技术和集成服务的方法。
一切都很顺利,直到我不得不开始更新数据。我正在使用 WebApi 2.1
问题是,我需要从角度更新 Employee 对象,但是当我传递一个 ID 和 Employee Json 对象时,当它到达 C# 代码时它没有正确反序列化。
这是我将数据从客户端传递到服务器时使用的代码:
employeeService.update({ id: $routeParams.employeeId }, { 'employee': result.object });
result.object 如下所示:
[prototype]: {...}
$id: "1"
$promise: {...}
$resolved: true
ContactNo: "0834541784"
Courses: [[object Object],[object Object]]
Division: {...}
DivisionId: 3
EmployeeCode: "E9955"
EmployeeId: 4
EmployeeType: null
EmployeeTypeId: 1
FirstName: "Gytis"
Gender: 2
IdNumber: "8311016262773"
Nationality: {...}
NationalityId: 1
Position: null
PositionId: 1
Surname: "Barzdukas"
它必须在 C# 中映射到的 Employee 类如下所示:
public class Employee
{
[Key]
public int EmployeeId { get; set;}
[Required]
public string EmployeeCode { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string IdNumber { get; set; }
public string ContactNo { get; set; }
public Gender Gender { get; set; }
public int? PositionId { get; set; }
public int NationalityId { get; set; }
public int? EmployeeTypeId { get; set; }
public int DivisionId { get; set; }
public Division Division { get; set; }
public ICollection<EmployeeCourse> Courses { get; set; }
public Position Position { get; set; }
public Nationality Nationality { get; set; }
public EmployeeType EmployeeType { get; set; }
}
现在,当代码到达 C# Web APi 更新函数时,我的 ID 填充了正确的 EmployeeID,但 Employee 参数为空白(空对象)。 当我将此类型从员工更改为对象时,代码如下所示,我无法将此对象解析为员工类型?
{
"employee": {
"EmployeeId": 1,
"EmployeeCode": "E9584",
"FirstName": "Carson",
"Surname": "Alexander",
"IdNumber": "8704167738384",
"ContactNo": "0834549632",
"Gender": 2,
"PositionId": 1,
"NationalityId": 1,
"EmployeeTypeId": 1,
"DivisionId": 3,
"Division": {
"DivisionId": 3,
"DivisionName": "Garden Court Milpark",
"Address1": null,
"Address2": null,
"Address3": null,
"PostalCode": null,
"ContactNo": null,
"RegionId": 1,
"EmployeeId": 0,
"CompanyId": 1,
"Company": null,
"Region": null,
"Employees": [
{}
]
},
"Courses": [
{
"EmployeeCourseId": 1,
"EmployeeId": 1,
"Employee": {},
"CourseId": 1,
"Course": {
"CourseId": 1,
"CourseCode": "Chem101",
"CourseDescription": "Chemistry"
},
"CourseStatus": "Distinction",
"CaptureDate": "2014-03-25T21:46:57.093",
"ExpiryDate": "2015-03-26T21:46:57.09",
"RegistrationDate": "2014-03-26T21:46:57.093"
},
{
"EmployeeCourseId": 2,
"EmployeeId": 1,
"Employee": {},
"CourseId": 3,
"Course": {
"CourseId": 3,
"CourseCode": "Macro401",
"CourseDescription": "Macroeconomics"
},
"CourseStatus": null,
"CaptureDate": "2014-03-26T21:46:57.093",
"ExpiryDate": null,
"RegistrationDate": null
},
{
"EmployeeCourseId": 3,
"EmployeeId": 1,
"Employee": {},
"CourseId": 4,
"Course": {
"CourseId": 4,
"CourseCode": "cal100",
"CourseDescription": "Calculus"
},
"CourseStatus": null,
"CaptureDate": "2014-03-26T21:46:57.093",
"ExpiryDate": null,
"RegistrationDate": null
}
],
"Position": null,
"Nationality": {
"NationalityId": 1,
"NationalityDescription": "African",
"EmployeeId": null
},
"EmployeeType": null
}
}
我希望有人可以帮助并告诉我如何将这个对象放入员工类
谢谢。
【问题讨论】:
标签: c# json serialization asp.net-web-api deserialization