Select Blazor:在使用@bind时如何使用中的onchange事件?

Select Blazor:在使用@bind时如何使用中的onchange事件?,select,bind,onchange,blazor,Select,Bind,Onchange,Blazor,我需要能够在中进行选择后运行函数。问题是我也绑定了@bind,当我尝试使用@onchange时,我得到了一个错误,说明@bind已经在使用它。我试着使用@onselectionchange,但没有任何效果,也没有运行该函数。我可以忘记@bind,只将@onchange赋值给函数,但我不确定如何将所选值传递给函数 我有以下代码: <select @bind="@SelectedCustID" @ @onchange="@CustChanged" class="form-control">

我需要能够在中进行选择后运行函数。问题是我也绑定了@bind,当我尝试使用@onchange时,我得到了一个错误,说明@bind已经在使用它。我试着使用@onselectionchange,但没有任何效果,也没有运行该函数。我可以忘记@bind,只将@onchange赋值给函数,但我不确定如何将所选值传递给函数

我有以下代码:

<select @bind="@SelectedCustID" @ @onchange="@CustChanged" class="form-control">
    @foreach (KeyGuidPair i in CustList)
    {
        <option value="@i.Value">@i.Text</option>
    }
</select>

谢谢。

这似乎是一个普遍的困惑。首先,您不能使用@onchange,因为@bind会在内部使用它。您应该能够从CustChanged属性的setter访问所选值。根据您试图对CustChanged执行的操作,您甚至不需要在更新此值时手动检查。例如,如果您的意图是在您的UI中直接或间接地在Linq或其他内容中使用CustChanged,则当您的更改时,UI将自动使用CustChanged值进行更新。因此,对于大多数用例,我认为不需要检查它何时更新

要使用@onchange,可以将其绑定到如下函数:

更新的更改事件参数上的公共无效 { 选择的var=e.值; } 如果使用foreach,可以完全避免@bind:


一些肮脏的细节:当我试图在服务器端Blazor中使用F时,出现了一些奇怪的行为。简言之,将选项列表设置为映射到记录列表的实体框架查询的结果不会@bind正确,但使用C类而非F记录的虚拟选项列表确实有效。但这并不是因为它是记录,因为如果我将列表设置为EF查询,然后立即将其设置为虚拟记录列表,它仍然没有@bind正确-但如果我注释掉EF行,它确实可以工作。

我的建议是,如果可能的话,在表单元素周围使用EditForm包装器。然后,您可以在一个位置检测任何表单元素的更改。例如,这适用于一组搜索过滤器。任何过滤器中的任何更改都会触发对数据的另一次查询等

如何触发表单更改事件的示例如下:


请检查这个例子。它正在使用@bind,但在设置值时会触发@onchange事件

客户名称 选择1 选择2 @bind本质上等同于同时具有value和@onchange,例如:

来源:

客户名称 瓦尔1 瓦尔2
只是附带说明:您可以使用@bind=SelectedCustID而不是@bind=@SelectedCustID。它更干净:一个字符@似乎是多余的。看到了吗,这是可行的,但如果你需要进行一些asyn调用,例如数据库更新,这将是一个问题。…@Alexandre,我认为你可以使用。等等,将异步调用转换为同步调用。请参阅。当用户要求选择代码时,它应为select not input。所选属性的true或false无效@Alexandre true。但是,Blazor会将选定的=true/false转换为有效的HTML。其他属性也一样,比如disabled:ahi-DharmaTurlte你能详细介绍一下@onchange=@x=>。。。。lambda表达式的用途是什么。我必须在那里编写什么代码来调用Methode?怎样Thx@nogood我填写了lambda。值得注意的是,此解决方案可与blazor EditForm一起使用,因为InputSelect无法以以下方式正确绑定事件:。这是所问问题的正确答案。
<select @bind="MyProperty">
<option>Your Option<option>
</select>

@code  {
    private string myVar;

    public string MyProperty
    {
        get { return myVar; }
        set
        {
            myVar = value;
            SomeMethod();
        }
    }

    private void SomeMethod()
    {
        //Do something
    }
}
<select @onchange=@(handleChange)>
    @foreach (var option in _options){
        <option value=@option.Id selected=@(SelectedId == option.Id)>@option.Name</option>
    }
</select>

@code {
    public record Person(int Id, string Name);
    public int SelectedId { get; set; }
    public List<Person> _options = new List<Person>() {
        new Person(1,"A"),
        new Person(2,"B"),
        new Person(3,"C")
    };

    public void handleChange(ChangeEventArgs args) {
        Console.WriteLine(args.Value);
        SelectedId = Int32.Parse(args.Value.ToString());
    }

}
@code { private string selectedItem {get; set;} private string CheckSelected { get { return selectedItem; } set { ChangeEventArgs selectedEventArgs = new ChangeEventArgs(); selectedEventArgs.Value = value; OnChangeSelected(selectedEventArgs); } } private void OnChangeSelected(ChangeEventArgs e) { if (e.Value.ToString() != string.Empty) { selectedItem = e.Value.ToString(); } } }
<input @bind="CurrentValue" />
<input value="@CurrentValue" @onchange="@((ChangeEventArgs e) => CurrentValue = e.Value.ToString())" />
<select value="@SelectedCustID" @onchange="@CustChanged" class="form-control">
    @foreach (KeyGuidPair i in CustList)
    {
        <option value="@i.Value">@i.Text</option>
    }
</select>