我正在尝试从MVC Web服务返回数据。我有model
:
namespace MyProject.Models
{
public class Item
{
int itemInfoId;
string itemDesc;
int itemTypeId;
DateTime itemDate;
DateTime itemTime;
float valueHigh;
float valueLow;
internal List<Item> getItemFeed2(string userNo, string itemDateFrom, string itemDateTo, string itemTimeFrom, string itemTimeTo, float valueLowA, float valueLowB, float valueHighA, float valueHighB, string itemTypes)
{ …
List<Item> itemFeed = new List<Item>();
try
{
while (sdr.Read())
{
itemFeed.Add(new Item
{
itemInfoId = (int)sdr.GetValue(0),
itemDesc = sdr.GetValue(4).ToString(),
itemTypeId = (int)sdr.GetValue(0),
itemDate = (DateTime)sdr.GetValue(5),
itemTime = (DateTime)sdr.GetValue(5),
//valueHigh = (float)sdr.GetValue(7),
//valueLow = (float)sdr.GetValue(8)
});
i++;
}
}
catch (Exception ex)
{
//Error
}
finally
{
Shared.closeDbConnection(sqlCon);
}
return itemFeed;
}
}
}
controller
:
public class ItemController : ApiController
{
public IEnumerable<Item> getItemFeed(string userNo, string itemDateFrom, string itemDateTo, string itemTimeFrom, string itemTimeTo, float valueLow, float valueHigh, string itemTypes)
{
Item evObj = new Item();
float valueLowA = valueLow - 10;
float valueLowB = valueLow + 10;
float valueHighA = valueHigh - 10;
float valueHighB = valueHigh + 10;
var itemFeed = evObj.getItemFeed2(userNo, itemDateFrom, itemDateTo, itemTimeFrom, itemTimeTo, valueLowA, valueLowB, valueHighA,valueHighB, itemTypes);
return itemFeed;
}}
从模型或控制器返回时itemFeed
对象显示count=15
这是正确的,因为有15条记录,但在浏览器窗口中,xml
只是空,这样没有显示数据:
<ArrayOfItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MyProject.Models">
<Item/>
<Item/>
<Item/>
<Item/>
<Item/>
<Item/>
<Item/>
<Item/>
<Item/>
<Item/>
<Item/>
</ArrayOfItem>
答案 0 :(得分:1)
私有成员无法执行XmlSerialization。将您的字段更改为公共字段或为其创建属性:
public int itemInfoId;
public string itemDesc;
public int itemTypeId;
public DateTime itemDate;
public DateTime itemTime;
public float valueHigh;
public float valueLow;