使用 onclick 函数传递参数时,Blazor 不刷新 UI

Blazor does not refresh UI when parameter is passed with onclick function

在 Blazor ServerSide Asp.net core 3.0 Preview 6 UI 中不刷新。 我已经改变了 counter.razor 作为例子。 如果单击按钮 "Set counter to ...",则不会刷新计数器。当您单击 "Click me" 按钮(没有参数)时,UI 会刷新,并且 1 会添加到上次单击的计数器中。

按钮似乎可以使用,但 UI 没有刷新。

@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>
<br />

<p>
    <button class="btn btn-primary" @onclick="@IncrementCount">Click me</button>
</p>

    @for (int i = 0; i < 5; i++)
    {
        var a = i;
        <p><button class="btn btn-primary" onclick="@(() => test(a))">Set counter to @a</button></p>
    }

    @functions {
        int currentCount = 0;

        protected void IncrementCount()
        {
            currentCount++;
        }

        void test(int i)
        {
            currentCount = i;
        }
    }

关于如何解决这个问题的任何建议,或者它是 Blazor 中的错误吗?

@functions { } 语法表明这是从 Preview5 开始的,但在 Preview6(当前)中,事件处理程序的语法 has changed:

Specifying event handlers in Blazor now uses the new directive attribute syntax instead of the normal HTML syntax. The syntax is similar to the HTML syntax, but now with a leading @ character. This makes C# event handlers distinct from JS event handlers.

  <button @onclick="@Clicked">Click me!</button>

When specifying a delegate for C# event handler the @ prefix is currently still required on the attribute value, but we expect to remove this requirement in a future update.

所以你需要@onclick="@(() => test(a))