【发布时间】:2021-05-10 22:06:48
【问题描述】:
我已经构建了一个 Web 程序集 Blazor 项目。
在其中一个 blazor 组件中,有一个用于提交新数据的表单。
这种形式,通过web api将数据POST到服务器以保存在数据库中。
<EditForm Model="@person">
<div class="col-12 row">
<label class="col-2 font-weight-bold">Id : </label>
<InputNumber class="form-control col-3" @bind-Value="person.Id" placeholder="id" />
</div>
<br />
<div class="col-12 row">
<label class="col-2 font-weight-bold">Name : </label>
<InputText class="form-control col-3" @bind-Value="person.Name" placeholder="name" />
</div>
<input type="submit" class="form-control btn btn-primary text-center" @onclick="@AddPerson" value="Add" />
</EditForm>
@AddPerson 背后的 C# 代码是这样的:
@code
{
public List<Person> people;
public Person person = new Person();
private async void AddPerson()
{
HttpResponseMessage httpResponse = await Http.PostAsJsonAsync<Person>("https://apisite.ir/api/people", person);
people = await Http.GetFromJsonAsync<List<Person>>("https://apisite.ir/api/people");
}
}
people 列表显示在这样的 foreach 表格中:
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (Person p in people)
{
<tr>
<td>@p.Id</td>
<td>@p.Name</td>
</tr>
}
</tbody>
</table>
问题是,当我点击 Add button 时,两个 api 调用都会发生,但 UI 中没有任何变化。
应该显示人员列表的表格不会更新。
我该怎么办?
【问题讨论】:
标签: c# asynchronous async-await blazor blazor-webassembly