0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MudTable を使っているときにゼロ除算が発生

Posted at

はじめに

事例として記録しておきます。

環境

コード

発生した例外

MudTableでゼロ除算が生じました。

System.DivideByZeroException: Attempted to divide by zero.
   at MudBlazor.MudTable`1.get_CurrentPageItems()
   at MudBlazor.MudTable`1.<BuildRenderTree>b__172_0(RenderTreeBuilder __builder2)
   at Microsoft.AspNetCore.Components.CascadingValue`1.Render(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Error: Unhandled exception in circuit '~'.

原因

@bind-RowsPerPageでバインドされた変数(初期値0)が明示的な初期化前に使われ、ページサイズがゼロになっていました。

        <MudTable Items="items" Dense Hover Striped Breakpoint="Breakpoint.Xs" @ref="_table"
            OnRowClick="EventCallback.Factory.Create<TableRowClickEventArgs<Book>> (this, arg => EditItem (arg.Item))"
            Filter="new Func<Book, bool>(FilterFunc)"
            SortLabel="⇅"
            AllowUnsorted=true
            @bind-RowsPerPage="_rowsPerPage"
            @bind-SelectedItem="selectedItem"
            @bind-SelectedItems="selectedItems" MultiSelection="@(allowDeleteItems && isSingleSession)">
    /// <summary>保存されたページ行数</summary>
    [CascadingParameter (Name = "RowsPerPage")]
    protected int RowsPerPage { get; set; }

    /// <summary>テーブルのページ行数</summary>
    protected int _rowsPerPage;

    /// <summary>初期化</summary>
    protected override async Task OnInitializedAsync () {
        await base.OnInitializedAsync ();
        _rowsPerPage = RowsPerPage;
    }

おわりに

同じ例外に遭遇された方のお役に立てば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?