列表在 Razor Pages 上显示为空,而它已满
List shows empty on Razor Pages, while it is full
我从 API 中获取了一些数据,我从中创建了一个列表,但是当我尝试遍历 .cshtml 文件中的列表时,它什么也没显示,列表的计数为 0 . 但是当我在 OnGetAsync() 方法中循环遍历列表时,它确实显示了结果。
我试过没有打开异步,我试图在 getDataAsync() 方法中填充列表。
public IList<Employee> Employee = new List<Employee>();
public async void OnGetAsync()
{
// Sets up HttpClient
await RunAsync();
// API call method which returns a list filled with the results
Employee = await GetDataAsync("api-url");
// Shows Results!
foreach (var item in Employee)
{
Debug.WriteLine(item.name);
}
}
static async Task<List<Employee>> GetDataAsync(string path)
{
string data = null;
List<Employee> list = new List<Employee>();
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
data = await response.Content.ReadAsStringAsync();
}
JObject json = JObject.Parse(data);
// Get the only the data i need from the entire json api result
foreach (var d in json["data"])
{
string setId;
string setName;
string setUrl;
if (d["id"] == null)
{
setId = "NA";
} else
{
setId = d["id"].ToString();
}
if (d["person"]["full_name"] == null)
{
setName = "NA";
} else
{
setName = d["person"]["full_name"].ToString();
}
if (d["avatar"]["url_small"] == null)
{
setUrl = "NA";
} else
{
setUrl = d["avatar"]["url_small"].ToString();
}
list.Add(new Employee
{
id = setId,
name = setName,
avatar_url = setUrl
});
}
Debug.Unindent();
return list;
}
<!-- Shows 0 -->
<p>@Model.Employee.Count</p>
<!-- Shows nothing -->
@foreach (var item in Model.Employee)
{
<p>@Html.DisplayFor(modelItem => item.name)</p>
}
我希望能够循环遍历列表并在网页上显示结果,但列表的计数为 0,并且什么也没有显示。
你的代码中的问题是 OnGetAsync
是一个异步方法,它应该 return 一个 Task
,而你 returning void
只需更改 return 类型即可。
public async Task OnGetAsync()
{
// Your code here
}
我从 API 中获取了一些数据,我从中创建了一个列表,但是当我尝试遍历 .cshtml 文件中的列表时,它什么也没显示,列表的计数为 0 . 但是当我在 OnGetAsync() 方法中循环遍历列表时,它确实显示了结果。
我试过没有打开异步,我试图在 getDataAsync() 方法中填充列表。
public IList<Employee> Employee = new List<Employee>();
public async void OnGetAsync()
{
// Sets up HttpClient
await RunAsync();
// API call method which returns a list filled with the results
Employee = await GetDataAsync("api-url");
// Shows Results!
foreach (var item in Employee)
{
Debug.WriteLine(item.name);
}
}
static async Task<List<Employee>> GetDataAsync(string path)
{
string data = null;
List<Employee> list = new List<Employee>();
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
data = await response.Content.ReadAsStringAsync();
}
JObject json = JObject.Parse(data);
// Get the only the data i need from the entire json api result
foreach (var d in json["data"])
{
string setId;
string setName;
string setUrl;
if (d["id"] == null)
{
setId = "NA";
} else
{
setId = d["id"].ToString();
}
if (d["person"]["full_name"] == null)
{
setName = "NA";
} else
{
setName = d["person"]["full_name"].ToString();
}
if (d["avatar"]["url_small"] == null)
{
setUrl = "NA";
} else
{
setUrl = d["avatar"]["url_small"].ToString();
}
list.Add(new Employee
{
id = setId,
name = setName,
avatar_url = setUrl
});
}
Debug.Unindent();
return list;
}
<!-- Shows 0 -->
<p>@Model.Employee.Count</p>
<!-- Shows nothing -->
@foreach (var item in Model.Employee)
{
<p>@Html.DisplayFor(modelItem => item.name)</p>
}
我希望能够循环遍历列表并在网页上显示结果,但列表的计数为 0,并且什么也没有显示。
你的代码中的问题是 OnGetAsync
是一个异步方法,它应该 return 一个 Task
,而你 returning void
只需更改 return 类型即可。
public async Task OnGetAsync()
{
// Your code here
}