I can't figure why I'm running into asynchronous issues, I have all async methods awaited.
Is there a better method I could be using to accomplish this?
I'm just trying to have a list of distinct values in a RadzenDropDownDataGrid for a Custom Filter.
I will need to do this multiple times for multiple columns, all the from the same Model/Context.
Steps to reproduce:
- Filter Title on any of the distinct Title Values
- Click Reset to reset the grid
That results in a DbContext in use error.
--If I unselect the active Title Filter, the Reset button works as expected.
I've tried using a DbContextFactory, but then I run into the opposite issue where the context is disposed so the grid no longer works.
Full repo:
This will run as-is on the SQL Server version of Northwind.
@page "/radzenTest"
@implements IDisposable
@using System.Linq.Dynamic.Core
@using Models
@using Microsoft.EntityFrameworkCore
@using RadzenDataGridFilterResetAsync.Data
@inject IDbContextFactory<NorthwindContext> NorthwindFactory
<div class="row">
<div class="col-md-2">
<label></label>
<RadzenButton Text="Reset" Click="@Reset" />
</div>
</div>
<RadzenDataGrid @ref="grid" Style="height: 1024px;" IsLoading=@isLoading Count="@count" Data="@employeeResults" LoadData="@LoadData"
AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterMode="FilterMode.Advanced"
AllowSorting="true" AllowPaging="true" PageSize="10" PagerPosition="PagerPosition.Top"
PagerHorizontalAlign="HorizontalAlign.Center" ShowPagingSummary="true" TItem="Employee" ColumnWidth="200px"
LogicalFilterOperator="LogicalFilterOperator.And">
<Columns>
<RadzenDataGridColumn TItem="Employee" Property="FirstName" Title="First Name" Width="140px" />
<RadzenDataGridColumn TItem="Employee" Property="LastName" Title="Last Name" Width="140px" />
<RadzenDataGridColumn TItem="Employee" Property="Title" Title="Title"
Type="typeof(IEnumerable<string>)" FilterValue="@selectedTitles" FilterOperator="FilterOperator.Contains" Width="150px">
<FilterTemplate>
<label id="lblFilterGroup" class="rz-grid-filter-label">Filter</label>
<RadzenDropDownDataGrid @ref="gridTitles" AllowRowSelectOnRowClick="false" AllowFiltering="true" AllowVirtualization="true"
FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" AllowClear="true" @bind-Value=@selectedTitles
Multiple="true" Placeholder="Select..." Data=@titles TextProperty="Title" ValueProperty="Title"
Style="width: 300px" Change="@OnSelectedTitlesChange">
<Columns>
<RadzenDropDownDataGridColumn Width="40px" Sortable="false">
<Template Context="data">
<RadzenCheckBox TriState="false" Value="@(selectedTitles != null && selectedTitles.Contains(((Employee) data).Title))"
TValue="bool" Change=@(args => { gridTitles.SelectItem(data); }) />
</Template>
</RadzenDropDownDataGridColumn>
<RadzenDropDownDataGridColumn Property="Title" Title="Title" Width="200px" />
</Columns>
</RadzenDropDownDataGrid>
</FilterTemplate>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
@code {
private RadzenDataGrid<Employee> grid;
private RadzenDropDownDataGrid<IEnumerable<string>> gridTitles;
private NorthwindContext? Context { get; set; }
private int count;
private IEnumerable<Employee> employeeResults;
private bool isLoading = false;
private List<Employee> titles = new List<Employee>();
private IEnumerable<string> selectedTitles;
protected override async Task OnInitializedAsync()
{
Context = await NorthwindFactory.CreateDbContextAsync();
var distinctTitles = await Context.Employees.Select(x => x.Title).Distinct().ToListAsync();
foreach (string title in distinctTitles)
{
titles.Add(new Employee { Title = title });
}
}
private void OnSelectedTitlesChange(object value)
{
if (selectedTitles != null && !selectedTitles.Any())
{
selectedTitles = null;
}
}
private async Task Reset()
{
selectedTitles = null;
grid.Reset(true);
await grid.FirstPage(true);
}
private async Task LoadData(LoadDataArgs args)
{
isLoading = true;
await Task.Yield();
IQueryable<Employee> query = Context.Employees.AsQueryable();
if (!string.IsNullOrEmpty(args.Filter))
{
query = query.Where(args.Filter);
}
if (!string.IsNullOrEmpty(args.OrderBy))
{
query = query.OrderBy(args.OrderBy);
}
count = await query.CountAsync();
employeeResults = await query.Skip(args.Skip.Value).Take(args.Top.Value).ToListAsync();
isLoading = false;
}
public void Dispose()
{
Context?.Dispose();
}
}