Christmas Sale! Limited Time Only - Enjoy 30% Off Metronic Extended License!
Buy for 669$  969$

Blazor - Select2 @onchange event not working as well @bind-Value not working


below code is not working, suggest me if i missed something.

<select id="ownerId"
@onchange="ValueChanged"
aria-label="Select a Head/Owner"
data-control="select2"
data-placeholder="Select a country..."
class="form-select form-select-solid form-select-lg fw-semibold">
@foreach (var user in Users)
{
<option value="@user.UserId">@user.Username</option>
}
</select>



private void ValueChanged(ChangeEventArgs e)
{
BiaVM.ProcessOwner = (int)e.Value;
}


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (4)


Hi,

We check your demo and it seems to be working fine.

Can you try the example below?


<select @onchange="onChange" class="form-select" data-control="select2" data-placeholder="Select an option">
<option></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>



@code {
private string value { get; set; }


private void onChange(ChangeEventArgs e){
value = e.Value.ToString();
}
}


Also as an alternative, you can just bind a value using the @bind attribute.

<select @bind="@value" class="form-select" data-control="select2" data-placeholder="Select an option">
<option></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>


Regards,
Lauris Stepanovs,
Keenthemes Support Team



I did exactly same thing as u suggested. and also i added below snippet code but i didnt get result
is there anything i should add more or doing wrong. please suggest me.
If i am using wrong version please suggest me in which version it is working

protected override void OnAfterRender(bool firstRender)
{
JS.InvokeVoidAsync("KTApp.init");
}



Hi,

This should work out of the box without any additional initialization.

Do you have this issue only with select2 inputs?

Can you try html plain select for test?

Regards,
Lauris Stepanovs,
Keenthemes Support Team



I'm having the same problem. The example is as follows.


<select class="form-select form-select-solid" data-control="select2" data-placeholder="Seçim yapınız..." data-allow-clear="true"
@onchange="@((ChangeEventArgs __e) => SelectedValue = __e?.Value.ToString())">
<option></option>
@foreach (var item in Select2Data)
{
<option value="@item.Id">@item.Name</option>
}
</select>

<span>Seçim value: @(SelectedValue)</span>

@code{
private string SelectedValue { get; set; } = "2";

protected override async void OnInitialized()
{

Select2Data = new List<Data>();
Select2Data.Add(new Data() { Id = "1", Name = "Liste 1" });
Select2Data.Add(new Data() { Id = "2", Name = "Liste 2" });
Select2Data.Add(new Data() { Id = "3", Name = "Liste 3" });
Select2Data.Add(new Data() { Id = "4", Name = "Liste 4" });
Select2Data.Add(new Data() { Id = "5", Name = "Liste 5" });
Select2Data.Add(new Data() { Id = "6", Name = "Liste 6" });
}

}


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(