【发布时间】:2015-04-02 12:26:42
【问题描述】:
我正在对我的 Web API 进行 ajax POST 调用并发布 JSON。下面是我的代码
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":[
]
}
现在的问题是当我发布 Sample 1 JSON 正确发布到控制器时。但在控制器中 MyExpirence 数组为空。但我正在获取 EId、EName 等的值。只有 MyExpirence 数组为空。
另一方面,如果我发布 Sample 2 向 JSON 添加一个虚拟数组,我将获得正确的值。在这种情况下,我得到了 EId、EName 甚至 MyExpirence 数组的正确值。
我不明白为什么我在示例 1 中的 MyExpirence 为空。我对示例 2 解决方案不满意。我想要一个正确的方法来做到这一点。
【问题讨论】:
-
不是答案,但
Expirence拼写为Experience -
你为什么要从
EmployeeBase派生Employee? -
贴出此类的代码:Expirence,MyExpirence[]中使用的那个
-
为什么您的体验与 EmployeeBase 相匹配?
标签: c# json asp.net-mvc-4 asp.net-web-api