Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Blazor将输入值绑定到oninput不使用onkeypress_C#_Html_Asp.net Core_Blazor_Asp.net Core 3.1 - Fatal编程技术网

C# Blazor将输入值绑定到oninput不使用onkeypress

C# Blazor将输入值绑定到oninput不使用onkeypress,c#,html,asp.net-core,blazor,asp.net-core-3.1,C#,Html,Asp.net Core,Blazor,Asp.net Core 3.1,我正在使用blazor进行搜索。当我在输入中按下一个键时,它会检查它是否是回车键,如果是,然后启动搜索。但是,绑定变量(关键字值)的值似乎在我连续两次按enter键之前不会更新。第一次按下时,该值不会更新 <h1>Blogs</h1> <fieldset> <label>Keyword Search</label> <input type="text" @bind="keywordValue" @bind:even

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

<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>

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

您应该使用Task而不是void

写入:
protected async Task关键字enterpressed
而不是
protected async void关键字enterpressed

顺便说一句,按钮元素的类型应该是“button”而不是“submit”

执行此操作:
而不是


希望这有帮助…

@onkeypress
显然是在绑定开始之前启动的;您可以尝试在
keywordValue
setter中实现您的逻辑
private string keywordValue { get; set; }
protected async void KeywordEnterPressed(KeyboardEventArgs eventArgs)
    {
        if (eventArgs.Key == "Enter")
        {
            await SearchBlogs();
        }
    }