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 (3)

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

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