问题描述
我要发送一个请求并返回所有信息,而不是发送多个HTTPget请求以从db获取所有数据。例如,我有几种型号:
public class Model1
{
public int Id { get; set; }
public string Address{ get; set; }
}
public class Model2
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Model3
{
public int Id { get; set; }
public string Food{ get; set; }
}
我需要将这些模型的列表(List
我创建一个新模型
public class Allinone
{
public List<Model1> Model1= new List<Model1>();
public List<Model2> Model2= new List<Model2>();
public List<Model3> Model3= new List<Model3>();
}
在控制器中,我有这个:
[HttpGet("updateall")]
public async Task<IActionResult> Updateall(int clientid)
{
Allinone model = new Allinone();
model.model1 = db.where(....);
model.model1 = db.where(....);
model.model1 = db.where(....);
return Ok(model);
}
在调试模式下,我可以看到该模型已正确填充了数据库中的数据,但是在客户端(在Postman和Xamarin客户端应用程序中),我无法读取内容。在邮递员中,我只得到空字符串{}。
我的Xamarin客户端应用程序具有与ASP Net Core相同的模型,但是内容为空。
解决方法
恕我直言,您不需要在AllInOne类中创建init列表,只需如下所示:
public class AllInOne
{
public List<Model1> Models1 {get; set;}
public List<Model2> Models2 {get; set;}
public List<Model3> Models3 {get; set;}
}
尝试将您的动作签名更改为此:
[Route("UpdateAll/{clientId}")]
public async Task<ActionResult<AllInOne>> UpdateAll(int clientid)