2015-04-02 36 views
0

我正在對我的web API進行ajax POST調用併發布JSON。下面是我的代碼在Web API中獲取複雜對象的問題發佈

C#類:

public class Employee:EmployeeBase 
{ 
    private string _EId; 
    private string _EName; 
    private string _Designation; 
    private EmployeeBase[] _Expirence; 

    public string EId 
    { 
     get { return _EId; } 
     set { _EId = value; } 
    } 
    public string EName 
    { 
     get { return _EName; } 
     set { _EName = value; } 
    } 
    public string Designation 
    { 
     get { return _Designation; } 
     set { _Designation = value; } 
    } 
    public Expirence[] MyExpirence 
    { 
     get { return _Expirence; } 
     set { _Expirence = value; } 
    } 
} 

public class EmployeeBase 
{ 
    private string _Id; 
    private string _Company; 
    private string _Years; 

    public string Id 
    { 
     get { return _Id; } 
     set { _Id = value; } 
    } 
    public string Company 
    { 
     get { return _Company; } 
     set { _Company = value; } 
    } 
    public string Years 
    { 
     get { return _Years; } 
     set { _Years = value; } 
    } 
} 

控制器:

[HttpPost] 
public ActionResult SaveAssociatDisassociateCameras(Employee zone) 
{ 
} 

樣品JSON:

示例1:

{ 
    "Id":"E100", 
    "Name":"TestEmployee", 
    "Designation":"Test Engineer", 
    "MyExpirence":[ 
     { 
     "Id":"01", 
     "Company":"Company1", 
     "Years":"10" 
     } 
    ] 
} 

示例2:

{ 
    "Id":"E100", 
    "Name":"TestEmployee", 
    "Designation":"Test Engineer", 
    "MyExpirence":[ 
     { 
     "Id":"01", 
     "Company":"Company1", 
     "Years":"10" 
     } 
    ], 
    "DummyArray":[ 

    ] 
} 

現在的問題是,當我張貼樣品1 JSON是越來越正確地發佈到控制器。但是在控制器MyExpirence數組中是null。但是我得到了EId,EName等的值。只有MyExpirence數組爲null。

另一方面,如果我發佈示例2添加一個虛擬數組到JSON我得到正確的值。在這種情況下,我爲EId,EName甚至MyExpirence數組獲得了合適的值。

我不明白爲什麼我會在樣本1中爲MyExpirence變爲空。而且我對樣本2解決方案並不滿意。我想要一個正確的方法來做到這一點。

+2

沒有答案的,但是'Expirence'拼寫'Experience' – Tom 2015-04-02 12:45:15

+0

你爲什麼要獲得從''EmployeeBase' Employee'? – 2015-04-02 12:55:01

+0

發佈此課程的教程:Expirence,MyExpirence中使用的[] – 2015-04-02 12:56:39

回答

0

您的代碼無法編譯:_ExpirenceMyExpirence屬性不同。修復它像下一個樣品

public class Employee:EmployeeBase 
{ 
    private string _EId; 
    private string _EName; 
    private string _Designation; 
    private EmployeeBase[] _Expirence; 

    public string EId 
    { 
     get { return _EId; } 
     set { _EId = value; } 
    } 
    public string EName 
    { 
     get { return _EName; } 
     set { _EName = value; } 
    } 
    public string Designation 
    { 
     get { return _Designation; } 
     set { _Designation = value; } 
    } 
    public Expirence[] MyExpirence 
    { 
     get { return _Expirence; } 
     set { _Expirence = value; } 
    } 
} 
相關問題