Blazor 将输入值绑定到 oninput 不适用于 onkeypress

Cal*_*leb 4 html c# asp.net-core blazor asp.net-core-3.1

我正在使用 blazor 进行搜索。当我按下输入中的某个键时,它会检查它是否是回车键,如果是,则启动搜索。但是,绑定变量(keywordValue)的值似乎不会更新,直到我连续两次按回车键。如果我第一次按下它,该值不会更新。

<h1>Blogs</h1>
<fieldset>
    <label>Keyword Search</label>
    <input type="text" @bind="keywordValue" @bind:event="oninput" @onkeypress="KeywordEnterPressed"/>
    <button type="submit" @onclick="SearchBlogs">Search</button>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
private string keywordValue { get; set; }
protected async void KeywordEnterPressed(KeyboardEventArgs eventArgs)
    {
        if (eventArgs.Key == "Enter")
        {
            await SearchBlogs();
        }
    }
Run Code Online (Sandbox Code Playgroud)

例如:如果我在输入字段中输入“test”并按 Enter 键,它将运行值为“”的 searchblogs()。当我再次按回车键时,它会运行 searchblogs() ,其值为“test”,就像它应该的那样。

小智 8

由于事件顺序,我通过使用“onkeyup”而不是“onkeypress”来实现此功能