我正在使用Angular/Web API應用程序。這是一個讓我自己在家裏忙碌的項目,這樣我就可以熟悉新技術和集成服務的方法。Deserialise Json在C#中的對象代碼
一切都很順利,直到我不得不開始更新數據。我正在利用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#中看起來像這樣:
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
}
}
我希望有人能請幫助,告訴我,我怎麼能得到這個對象爲Employee類
感謝。
謝謝你的建議。我嘗試了上面發佈的兩條建議,但仍然沒有運氣。我不能理解的是,如果我將類型設置爲對象,我會將值返回到參數中。然後,當我把它設置爲類型員工我只是得到一個對象的所有屬性爲空/空。 – Gremlin1708
嗯,那麼問題出在客戶端發佈。您需要在MVC操作方法中強制輸入參數。你見過在Fiddler中發佈的JSON嗎? 問題可能是其中一個字段未正確反序列化。像「abc」被綁定到一個int。您是否嘗試將Employee類更改爲只有幾個字段? – schaefea
我改成了一個簡單的Employee類,現在只有一個firstName和Surname,而當JSON到達服務器時,參數仍然是空的。我已將代碼提交給GitHub [link]([email protected]:Ruandv/Training.git)angular_ui分支。 – Gremlin1708