ASP.NET MVC:如何从2个API获取数据,如何将数据异步绑定到UI上的一个表?

问题描述

我们有一个这样的模型类:

public class TempClass
{
        public string col1 {get; set;}
        public string col2 {get; set;}
        public string col3 {get; set;}
        public string col4 {get; set;}
        public string col5 {get; set;}
        public string col6 {get; set;}
}

这是控制器中从两种不同服务获取数据的操作方法

public JsonResult GetResult(int id)
{
    List<TempClass> tc = new ListTempClass>();
    
    //Step1: Get data from serviceA for col1,col2 and col3
    tc = this.serviceA.GetDataFromServiceA(id);
    
    //Step2: Call serviceB to get data for col4,col5 and col6 with col1 value which we get from step1
    foreach (var t in tc)
    {
        var tmp = this.serviceB.GetDataFromServiceB(t.col1);

        if(tmp != null)
        {
           t.col5 = tmp.p5;
           t.col4 = tmp.p4;
           t.col6 = tmp.p6;
        }
    }
    
    //Step3: return back
    return new JsonResult() { Data = tc };
}

如您所见,当前问题是,我们需要等待第一个API调用完成,然后再调用第二个API以使用从第一个API获得的值返回数据,这非常慢。

我想知道一旦第一个API调用完成后,是否有某种方法可以将数据绑定到UI ..然后在第二个API之后将数据异步绑定到UI?

还有其他建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)