简体   繁体   English

提交EditForm后,组件不重新渲染

[英]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.

相关问题 EditForm 验证未将无效类应用于自定义组件 - EditForm validation not applying invalid class to custom component 如何制作一个有 EditForm 的组件并将表单和验证封装在里面? - How to make a component that have an EditForm and encapsulate the form and the validation inside? 提交更改后,DataGridView不更新C#LINQ - DataGridView does not update after submitting changes c# linq Blazor:如何在 EditForm.Model 更改后专注于输入 - Blazor: How to keep focus on an input after EditForm.Model changes devExpress GridView EditForm不允许更新图像字段 - devExpress GridView EditForm does not allow image fields to be updated Blazor:使用 EditForm-Component 加载页面时 _Host.cshtml 中的 NullReferenceException - Blazor: NullReferenceException in _Host.cshtml when loading a page with EditForm-Component Blazor EditForm DataAnnotationsValidator 在自定义子组件更改时意外验证 state - Blazor EditForm DataAnnotationsValidator validates unexpectedly when a custom child component changes state Blazor EditForm 验证仅在使用“验证器组件”(在“业务逻辑验证”功能中)时部分起作用 - Blazor EditForm validation only partially works when using a 'Validator component' (within a 'business logic validation' function) 当 url 查询更改时强制 Blazor 组件重新呈现 - Force Blazor component to rerender when url query changes StateHasChanged 用于特定组件,而不是重新渲染页面中的所有组件 - StateHasChanged for specific component instead rerender all components in a page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM