将列表从c#发送到角度

问题描述 投票:0回答:1

我有后端C#API

public async Task<Object> GetTestType()
{
    List <TestType> TestList = _context.TestTypes.ToList();
    return TestList ;
}

这里是TestType类

public class TestType
{
    public int id{ get; set; }
    public string var1{ get; set; }
    public string var2{ get; set; }
}

我有角度

this.TestService.getTestType().subscribe(
  (res:any)=>
  {
    console.log(res) ; 

    this.RideTypeModelData.rideTypeID = res.rideTypeID;
    console.log('rideTypeID-'+this.RideTypeModelData.rideTypeID) ; 
  },

如果是console.log(res)我得到

[[Object Object]

我也用角创建了TestModel

export class TestTypeModel{
    id: Number;
    typeWebID: String;
    var1: String;
    var2: String;
}

并且正在尝试以此来获取对象数据公共TestTypeModelData:TestTypeModel;

this.TestService.getTestType().subscribe(
  (res:any)=>
  {
    this.TestTypeModelData.id= res.id;
    console.log(this.TestTypeModelData.id) ; 
  },

我得到错误

core.js:15723错误TypeError:无法设置未定义的属性'id'

我如何转移该List对象并以有角度的方式输出它?

c# angular list model data-transfer
1个回答
0
投票

尝试解析响应:

© www.soinside.com 2019 - 2024. All rights reserved.