DataGridMultipleSelection - problem converting @bind-Value=@multipleValues

I have a similar problem as mentioned here:

I need to get the selected items from the grid and pass them to another method.

I need to make my bind-value an object or else it won't compile.
Logging this object in the console log reveals it contains a list of my selected items.
I'm struggling with how to convert this object to the List<MyClass> parameter I expect.

I tried numerous options like casting to List<MyClass>, IList<MyClass>, IEnumerable<MyClass>, using .ToList() on the variable but all I get are either compiletime errors or runtime errors saying I'm not casting right.

The example you've provided in the documentation is only storing the selection but is not doing anything with it.

I would really appreciate if you could explain how to continue with the selected items from the grid.

The example is here and it's fully operational:

You can get the project to play with it.

I'm now not converting the multivalues variable but I'm just passing it as an object to my controller.
My controller is converting it to a proper list of MyClass.
I can now continue.

1 Like

Hi Paul, can you please share the code? I have the same problem.

Razor page:
<RadzenGrid @ref="grid" AllowSorting="true" Data="@ItemList" TItem="TItemDetails" ColumnWidth="200px" SelectionMode="DataGridSelectionMode.Multiple" @bind-Value="@SelectedItems">

Code behind:
protected object SelectedItems;

My client-side service is sending it to my controller:

public async Task<List<T>> CopyItems(int id, object items)
{
    using var httpClient = _httpClientFactory.CreateClient(ConstantsHelper.ApiClientName);
    using var httpResponse = await httpClient.PostAsJsonAsync($"{_controllerName}/{ConstantsHelper.Copy}/{id}",
        items, options: new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
    if (!httpResponse.IsSuccessStatusCode) return null;

    await using var contentStream = await httpResponse.Content.ReadAsStreamAsync();
    var response = await JsonSerializer.DeserializeAsync<OperationResponseList<List<T>>>(contentStream,
        new JsonSerializerOptions { IgnoreNullValues = true, PropertyNameCaseInsensitive = true });
    return response?.Records;
}

My controller:

[HttpPost( ConstantsHelper.Copy + "/{id:int}")]
public async Task<IActionResult> CopyItems(int id, [FromBody] List<MyClass> items)
{
    var result = await _service.CopyItems(id, items);
    if (result.IsSuccess)
        return Ok(result);
    return BadRequest(result);
}

Because I have a Blazor WASM front-end, the payload (in this case the selected items) is sent to my controller as JSON. The controller is converting it back for me to a List of my class.

@Alberto: I hope this helps you.

OK. Thanks for the code. I will try to implement it.