我想我不能在标题中更好地解释它。 对不起。
我返回一个包含和ID
和一个name
的元组。
List<Tuple<int, string>> tupleList= new List<Tuple<int, string>>();
foreach (Person person in people)
tupleList.Add(person.GetIdName());
GetIdName
在 People
类中定义如下:
public Tuple<int, string> GetIdName()
{
return new Tuple<int, string>(ID, nome);
}
现在,所有这些都是由客户端通过 API 在此调用的
[ResponseType(typeof(List<Tuple<int, string>>))]
public IHttpActionResult GetPeople(int id)
{
return Ok(tupleList)
}
我在客户端得到的是:
[{"m_Item1":1,"m_Item2":"Name 1"},{"m_Item1":2,"m_Item2":"Name 2"}]
有没有办法将m_Item1
设置为其他内容?
如果您能够更改定义,请创建如下所示的视图模型,
public class MyViewModel
{
public int Id { get; set; }
public string name { get; set; }
}
所以你的代码将是,
List<MyViewModel> tupleList= new List<MyViewModel>();
foreach (Person person in people)
tupleList.Add(person.GetIdName());
同时相应地更改方法GetIdName
以返回MyViewModel
对象