question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor cannot execute OnChange Event

See original GitHub issue

Hello, I have an issue with the InputSelect tag, I want to execute OnChange handler and pass additional parameters into it, but situation is that, the OnChange method is successfully completed but the selected value of the target drop-down was deselected. I upload my code. The problem is hit on the first InputSelect

ValueChanged="@((UserRoleType value) => this.GetUsersInOutOfRole(value, false))"

@page "/AdministrationPanel"
@attribute [Authorize(Roles = GlobalConstants.AdministratorRole)]
@inject IAdministrationService administrationService

<PageTitle>Administration Panel</PageTitle>

<h3 class="pageHeading">Administration Panel</h3>
<hr />
<div class="row">
    <div class="col-12 col-sm-12">
        <h4 class="sectionHeading">Add User In Role</h4>
    </div>
</div>

<EditForm Model="@this.model" OnValidSubmit="@(async () => await this.AddUserInRole())">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <div class="form-group">
        <div class="row">
            <div class="col-5 col-sm-5">
                <label>Role Type</label>
                <InputSelect
                 class="form-control"
                 ValueExpression="@(() => this.model.RoleType)"
                 Value="@this.model.RoleType"
                 ValueChanged="@((UserRoleType value) => this.GetUsersInOutOfRole(value, false))">
                    <option value="" selected disabled>Select role...</option>
                    @foreach (var roleType in ((UserRoleType[])Enum.GetValues(typeof(UserRoleType))).OrderBy(x => x.ToString()))
                    {
                        <option value="@roleType">@(roleType.ToString().SplitCamelCase())</option>
                    }
                </InputSelect>
                <ValidationMessage For="@(() => this.model.RoleType)" />
            </div>
            <div class="col-5 col-sm-5">
                <label>Username</label>
                <InputSelect class="form-control" @bind-Value="this.model.UserName">
                    <option value="" selected disabled>Select user...</option>
                    @foreach (string username in this.usernames.OrderBy(x => x.ToString()))
                    {
                        <option value="@username">@(username)</option>
                    }
                </InputSelect>
                <ValidationMessage For="@(() => this.model.UserName)" />
            </div>
        <div class="col-2 col-sm-2">
            <button class="btn btn-primary" type="submit">Add</button>
        </div>
        </div>
    </div>
</EditForm>

@code {
    AddRemoveUserToFromRoleInputModel model = new AddRemoveUserToFromRoleInputModel();
    List<string> usernames = new List<string>();

    private async Task AddUserInRole()
    {
    }

    private async Task GetUsersInOutOfRole(UserRoleType roleType, bool inRole)
    {
        this.usernames =await this.administrationService.GetUsernamesInOutOfRole(roleType, inRole);
        this.StateHasChanged();
    }
}

Regards, Simeon Valev

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
MariovanZeistcommented, Dec 9, 2021

You aren’t updating the this.model.RoleType value inside the GetUsersInOutOfRole function. This will result in this.model.RoleType never changing.

and might be what’s causing your issue.

1reaction
TanayParikhcommented, Dec 9, 2021

Thanks @MariovanZeist, marking resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

onChange event not firing Blazor InputSelect
The problem is the @bind attribute makes use of the @onchange event and Blazor will not allow multiple @onchange event handlers.
Read more >
Executing async operations onchange in Blazor
When you use the @bind or @bind-Value directive, Blazor generates an onchange event handler behind the scenes which is used to update the...
Read more >
Cannot bind value of element and have onchange event ...
If I can't have bind and have an onchange for a single dropdown then I need to figure out a way to call...
Read more >
ASP.NET Core Blazor event handling
Delegate event handlers automatically trigger a UI render, so there's no need to manually call StateHasChanged . Exceptions are logged. The ...
Read more >
Trying to use TelerikEditor OnChange="@SomeMethod"
While the OnChange event is not something that the Editor supports (in Blazor and in other suites), exposing an OnBlur event seems like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found