Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor: How to use the onchange event in <select> when using @bind also?

I need to be able to run a function after a selection in made in a <select>. The issue is I'm also binding with @bind and I get a error when I try to use @onchange stating that it is already in use by the @bind. I tried using @onselectionchange, but that does nothing(doesn't run the function). I could forget the @bind and just assign @onchange to a function, but I'm not sure how to pass the selected value to the function.

I have the following code:

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

Thanks.

like image 908
Mike Avatar asked Sep 26 '19 23:09

Mike


People also ask

How do you get the selected value of dropdown in Blazor?

To retrieve a selected value from the select control, you can use either the @bind or @onchange event.

How do you bind a dropdown in Blazor?

Bind to a Model To bind the DropDownList to a model: Populate its Data parameter with the collection of items you want in the dropdown. Set the TextField and ValueField parameters to point to the corresponding property names of the model. Set the Value property to the intial value of the component (optional).

What does @bind do Blazor?

Bind a property or field on other Document Object Model (DOM) events by including an @bind:event="{EVENT}" attribute with a DOM event for the {EVENT} placeholder. The following example binds the InputValue property to the <input> element's value when the element's oninput event ( input ) is triggered.

What is the difference between bind and bind value in Blazor?

In Blazor, there is no significant difference between using these two attributes. The @bind attribute is a shorthand of the @bind-value attribute and its delegate will trigger the ValueChanged event of the component.


2 Answers

<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     } } 
like image 84
Zsolt Bendes Avatar answered Oct 22 '22 14:10

Zsolt Bendes


@bind is essentially equivalent to the having both value and @onchange, e.g.:

<input @bind="CurrentValue" /> 

Is equivalent to:

<input value="@CurrentValue" @onchange="@((ChangeEventArgs e) => CurrentValue = e.Value.ToString())" /> 

Since you've already defined @onchange, instead of also adding @bind, just add value to prevent the clash:

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

Source: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-3.1

like image 32
Saeb Amini Avatar answered Oct 22 '22 14:10

Saeb Amini