简体   繁体   English

Blazor 元素在首次加载时不会更新

[英]Blazor element will not update on first load

I have a section of a page in Server-side blazor that will not update on the first load.我在服务器端 blazor 中有一部分页面在第一次加载时不会更新。 After the page is reloaded it updates with no issues and acts as intended.重新加载页面后,它会毫无问题地更新并按预期运行。 Any guidance will be much appreciated.任何指导将不胜感激。

The first time someone presses the Open or Close button, it runs the code successfully but does not update the UI so that if it is pressed again, it throws an error.当有人第一次按下 Open 或 Close 按钮时,它会成功运行代码,但不会更新 UI,因此如果再次按下它,则会引发错误。 But, as I said previously, if the same page is simply refreshed, then I can click Open/Close to my heart's content and it updates the UI and allows for the proper flip of the switch.但是,正如我之前所说,如果只是刷新了相同的页面,那么我可以单击打开/关闭到我心中的内容,它会更新 UI 并允许正确翻转开关。

<div class="row row-padding mt-5 d-flex justify-content-between">
    <h5 class="secondary-font">Month End</h5>
    </div>
    <hr />
    <div class="row row-padding">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Month</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach(var date in OpenMonths)
                {
                    <tr>
                        <td class="align-middle">@date.ToString("MMMM, yyyy")</td>
                        @if (PropertyMonthCloseds.Exists(p => p.Month == date && !p.IsOpen)) 
                        {
                            <td class="align-middle">Closed</td>
                            <td class="align-middle">
                                <button type="button" class="btn btn-link accent-font" @onclick="@(async () => await OpenCloseMonth(date))">Open</button>
                            </td>
                        }
                        else
                        {
                            <td class="align-middle">Open</td>
                            <td class="align-middle">
                                <button type="button" class="btn btn-link accent-font" @onclick="@(async () => await OpenCloseMonth(date))">Close</button>
                            </td>
                        }
                    </tr>
                }
            </tbody>
        </table>
    </div>
private async Task OpenCloseMonth(DateTime date)
    {
        try
        {
            if (PropertyMonthCloseds.Exists(p => p.Month == date))
            {
                var prop = PropertyMonthCloseds.FirstOrDefault(p => p.Month == date);
                prop.IsOpen = !prop.IsOpen;
                await Repo.UpdatePropertyMonthClosed(prop);
                Toast.ShowSuccess("Month End change saved.");
            }
            else
            {
                var now = DateTime.UtcNow;
                var newProp = new PropertyMonthClosed
                {
                    Month = date,
                    CreatedBy = User.UserId,
                    ModifiedBy = User.UserId,
                    CreatedOn = now,
                    ModifiedOn = now,
                    PropertyId = PropId
                };

                await Repo.CreatePropertyMonthClosed(newProp);
                Toast.ShowSuccess("Month Closed.");
            }
            await ProcessOpenMonths();
        }
        catch(Exception ex)
        {
            ShowInnerException(ex);
        }
    }
    private async Task ProcessOpenMonths()
    {
        var subs = await Repo.GetSubmissions(PropId);
        OpenMonths = subs.GroupBy(x => new { Month = x.SubmissionDate.Month, Year = x.SubmissionDate.Year })
            .OrderByDescending(o => o.Key.Year).ThenByDescending(t => t.Key.Month)
            .Select(s => new DateTime(s.Key.Year, s.Key.Month, 1))
            .ToList();
    }

Please add the method StateHasChanged() to the end of your OpenCloseMonth() function.请将 StateHasChanged() 方法添加到 OpenCloseMonth() function 的末尾。 That should refresh the UI immediately.那应该立即刷新 UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM