2

I try to create the user management in blazor. The check box is check/uncheck when I click on it. but when It showed index out of bound. I don't know what went wrong. just try with blazor wasb. please help check this. it is just a basic component but somehow I don't get used to its usage yet.
I try to create the user management in blazor. The check box is check/uncheck when I click on it. but when It showed index out of bound. I don't know what went wrong. just try with blazor wasb. please help check this. it is just a basic component but somehow I don't get used to its usage yet.

  @page "/manageuserrole/{userId}"
    @inject HttpClient client

@inject IJSRuntime js
@inject NavigationManager uriHelper

<h3>User Roles</h3>


@if (manageUserRolesDto == null)
{
    <text>Loading...</text>
}
@*else if (manageUserRolesDto.Length == 0)
    {
        <text>No Records Found.</text>
    }*@
else
{
    <EditForm Model="@manageUserRolesDto" OnValidSubmit="@UpdateUserRoles">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Role</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < manageUserRolesDto.UserRoles.Count(); i++)
                {
                    <tr>
                        <td>@manageUserRolesDto.UserRoles[i].RoleName</td>
                        <td>
                            <div class="form-check m-1">
                                <input type="checkbox" 
                                       @bind="@manageUserRolesDto.UserRoles[i].Selected" 
                                       />
                            </div>
                        </td>

                    </tr>
                }

            </tbody>
        </table>
        <button type="submit" class="btn btn-success">
            Submit
        </button>
    </EditForm>


}
@code {
    [Parameter]
    public string userId { get; set; }

    ManageUserRolesDto manageUserRolesDto { get; set; }
    protected override async Task OnInitializedAsync()
    {
        manageUserRolesDto = await client.GetFromJsonAsync<ManageUserRolesDto>("api/userroles/" + userId);
    }

    private void checkUserRole(int i)
    {
        manageUserRolesDto.UserRoles[i].Selected = !manageUserRolesDto.UserRoles[i].Selected;

    }



    async Task UpdateUserRoles()
    {
        await client.PutAsJsonAsync("api/userroles/" + userId, manageUserRolesDto);
        uriHelper.NavigateTo("user");

    }


    async Task ManagePermission(string roleId)
    {

    }
}
l.b.vasoya
  • 1,188
  • 8
  • 23
Sras
  • 1,686
  • 2
  • 19
  • 29

1 Answers1

4
@for (int i = 0; i < manageUserRolesDto.UserRoles.Count(); i++)
{
    int copy = i; 
    <tr>
    <td>@manageUserRolesDto.UserRoles[i].RoleName</td>   <-- this 'i' is OK
    <td><div class="form-check m-1">
            <input type="checkbox" 
              @bind="@manageUserRolesDto.UserRoles[copy].Selected"  <-- i is not OK
            />
    </div></td>
    </tr>
}

The @bind is compiled to a lambda function that captures the variable.

Another option is to use a foreach() { } instead of a for() { }

H H
  • 263,252
  • 30
  • 330
  • 514
  • This is working. could you clearly explain why this happend? and how can I use foreach{} – Sras Jan 30 '21 at 02:08
  • No, this has been answered and explained many times already. Google "C# lambda capture the loop variable" – H H Jan 31 '21 at 08:54