在 Blazor 中取消事件 (onchange) 或检索旧值

Sam*_*jan 5 c# asp.net blazor

我试图在捕获所选值后取消元素的“onchange”事件。基本上只是为了捕获选定的值并将选定的元素返回到它所在的位置。我怎样才能在 Blazor 中做到这一点?

<select @onchange="onSelectFilter">
 <option value="0">Select a filter</option>
 <option value="1">Filter 1</option>
 <option value="2">Filter 2</option>
</select> 

@code
{

 private void onSelectFilter(ChangeEventArgs args)
 {
   var val = int.Parse(args.Value.ToString());

   if (val != 0)
   {
     //do something

     //reset the value to 0
     args.Value = 0;
    }
   }

 }
Run Code Online (Sandbox Code Playgroud)

Dav*_*son 3

只需使用 C# 即可实现更简单的方法

@page "/"

<select @bind="Selected" class="form-control">
 <option value="0">Select a filter</option>
 <option value="1">Filter 1</option>
 <option value="2">Filter 2</option>
</select> 

<h1>@Captured</h1>

@code
{
    int Captured;

    int _Selected;
    private int Selected
    {
        get{return _Selected;}
        set{
             Captured = value; //store the captured value
             _Selected = 0;   //reset the drop down
           }
    }

}
Run Code Online (Sandbox Code Playgroud)

你可以使用这个 @code 块变得更加简单

@code
{
    int Captured;

    int Selected
    {
        get{return 0;}  // reset the select
        set{Captured = value;} //store the captured value
    }
}

Run Code Online (Sandbox Code Playgroud)