[英]After submitting EditForm, component does not rerender
Good day!再会!
I use Blazor component for rendering and updating non-sql database information.我使用 Blazor 组件来渲染和更新非 sql 数据库信息。 I expected the re-rendering of the component after submission, but, even after calling this.StateHasChanged();
我希望在提交后重新渲染组件,但是,即使在调用this.StateHasChanged();
, I have to manually refresh the page. ,我必须手动刷新页面。 My code does not have errors or warning messages.我的代码没有错误或警告消息。
After reading the official Blazor and ASP.net documentation, lots of tutorials and answers on stackoverflow, I still can not figure why the component is not refreshing.在阅读了官方的 Blazor 和 ASP.net 文档,大量关于 stackoverflow 的教程和答案后,我仍然不明白为什么组件不刷新。
I also manually tried to refresh the component's content using a control variable and if()
statements.我还手动尝试使用控制变量和if()
语句刷新组件的内容。
Thank you!谢谢!
@page "/admin/selectcategory"
<h5>SelectCategory</h5>
@{
int count = categoryList.Count;
if (count != 0)
{
<div class="list-group list-group-flush mt-3 mb-5">
@foreach (var rec in categoryList)
{
<a href="Category/@rec.ID" class="list-group-item list-group-item-action pl-0 py-4">
<div class="d-flex justify-content-between">
<h5 class="mb-1 mt-2">@rec.Name</h5>
<small>500 anunturi</small>
</div>
<p class="mt-1">@rec.Description</p>
<small class="text-muted">Lorem, Ipsum, Dolor sit, Amet...</small>
</a>
}
</div>
}
<h3 class="panel-title my-2">Add Category</h3>
<p>Lorem ipsum dolor sit amet</p>
<EditForm Model="@newCategory" OnValidSubmit="AddCategory"
class="my-3">
<InputText @bind-Value="newCategory.Name"
class="form-control py-3 px-3 my-2"
id="new-category-name"
placeholder="Category Name" />
<InputTextArea @bind-Value="newCategory.Description"
class="form-control py-3 px-3 my-2"
id="new-category-description"
placeholder="Description" />
<button type="submit" class="btn btn-light py-2 px-3 my-2">Create Category</button>
</EditForm>
}
@code{
private List<Category> categoryList = new List<Category>();
private Category newCategory = new Category();
private Category cat = new Category();
protected override async Task OnInitializedAsync() => categoryList = await Task.Run(() => cat.Read("CategoryList"));
private async Task AddCategory()
{
await Task.Run(() => newCategory.Create("CategoryList"));
newCategory = new Category();
this.StateHasChanged();
Your not adding the item to the list after create.创建后您没有将项目添加到列表中。 Or you should call the same code you have in OnInitializedAsync() again.或者您应该再次调用 OnInitializedAsync() 中的相同代码。 Which is why it is showing on refresh.这就是为什么它会在刷新时显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.