2014-03-31 68 views
0

我正在使用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參數爲空(空對象)。 empty Parameter.. 當我從僱員改變這種類型的對象代碼看起來像這樣,我不能得到這個對象解析到類型員工? enter image description here

{ 
    "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類

感謝。

回答

0

我想通了。問題在於我通過我的對象的方式,以及我設置WebApi路由的方式。經過一些閱讀後,我改變了這些東西,現在一切正常。

感謝您的所有幫助和建議。

我改變了的WebAPI路線起點:

[Route("Employee/{id}/{employee}")] 
[HttpPut] 
public HttpResponseMessage PutEmployee(int id, EmployeeDto employee) 
{...}  

TO

[Route("Employee/{id}")] 
[HttpPut] 
public HttpResponseMessage PutEmployee(int id, EmployeeDto employee) 
{...} 

,然後在我的UI我改變了代碼如下:從

角控制器:

employeeService.update({ id: $routeParams.employeeId }, {employee: result.object}); 

employeeService.update({ id: $routeParams.employeeId }, result.object); 

我改變了我的EmployeeService(更新)看起來像現在這樣:

update: { 
     method: "PUT", params: { 
     EmployeeCode: "Default", 
     FirstName: "Jan", 
     Surname: "Solvent", 
     IdNumber: "8508174141444", 
     ContactNo: "0834411252", 
     Gender: "1", 
     NationalityId: "1", 
     DivisionId: "1" 
    } 
0

您不能在MVC中發佈'object'類型。您需要發佈Employee類型:

public HttpResponseMessage PutEmployee(int id, Employee employee) 
{ 
    ... 

您可能會遇到的命名問題,需要重命名第二個參數不具有相同的名稱作爲類型:

public HttpResponseMessage PutEmployee(int id, Employee viewModel) 
{ 
    ... 
+0

謝謝你的建議。我嘗試了上面發佈的兩條建議,但仍然沒有運氣。我不能理解的是,如果我將類型設置爲對象,我會將值返回到參數中。然後,當我把它設置爲類型員工我只是得到一個對象的所有屬性爲空/空。 – Gremlin1708

+0

嗯,那麼問題出在客戶端發佈。您需要在MVC操作方法中強制輸入參數。你見過在Fiddler中發佈的JSON嗎? 問題可能是其中一個字段未正確反序列化。像「abc」被綁定到一個int。您是否嘗試將Employee類更改爲只有幾個字段? – schaefea

+0

我改成了一個簡單的Employee類,現在只有一個firstName和Surname,而當JSON到達服務器時,參數仍然是空的。我已將代碼提交給GitHub [link]([email protected]:Ruandv/Training.git)angular_ui分支。 – Gremlin1708