我正在尝试从返回数据集的 Web 服务创建 JSON 响应。如何遍历数据表并将这些值放入客户端模型中?当我尝试遍历数据表时,我收到无效的启动器错误。这是我的代码:
public class ClientsController : ApiController
{
public IEnumerable<ClientResponseModels> GetAllClients()
{
//retrieves list of all the clients
var dsClients = ClientRepository.GetAllClients();
//create a json response object
ClientResponseModels[] response = new ClientResponseModels[]
{
new ClientResponseModels
{
UserId= userDetails.UserID,
UserName=userDetails.LoginName,
LocationName=centreLocation.ToString(),
Clients = new List<ClientModels>
{
foreach(DataRow row in dsClients.Tables[0].rows)
{
new ClientModels //these values need to come from datatable
{
Id =Convert.ToInt32(row["client_id"]),
ClientName=Convert.ToString(row["client_nme"])",
DOB= Convert.ToDateTime(row["birth_date]")
}
}
}
}
};
return response;
}
else {
throw new HttpResponseException(HttpStatusCode.Forbidden);
}
}