Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuickGrid collection from OnParametersSet not working with static SSR #55915

Closed
1 task done
guardrex opened this issue May 28, 2024 · 1 comment
Closed
1 task done
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@guardrex
Copy link
Contributor

guardrex commented May 28, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I don't think that this is the same as Add support to the QuickGrid paginator for static server rendering (dotnet/aspnetcore #51249), but it might have the same cause.

When scaffolding into a BWA that only adopts static SSR, the following code works ✔️ ...

@inject BlazorWebAppMovies.Data.BlazorWebAppMoviesContext DB

...

<QuickGrid Class="table" Items="DB.Movie">

... and the following works, too ✔️ (ignore NRT static analysis, as this is for only for discussion here, and assume a QS is provided e.g., ?movieSearch=XXX) ...

@inject BlazorWebAppMovies.Data.BlazorWebAppMoviesContext DB

...

<QuickGrid Class="table" Items="DB.Movie.Where(s => s.Title!.Contains(TitleSearch))">

...

[SupplyParameterFromQuery]
public string TitleSearch{ get; set; }

... but the following no-ops ❌. The QG doesn't display data, but the collection (movies) does receive the correct DbSet from the dB in OnParametersSet. Why? ... and what's the correct approach that I can use with the new Blazor tutorial (static SSR) for this search/filter scenario?

This ...

<QuickGrid Class="table" Items="@(!string.IsNullOrEmpty(TitleSearch) ? DB.Movie.Where(s => s.Title!.Contains(TitleSearch)) : DB.Movie)">

... is a working approach ✔️ ... or to deal with the nullable Title ...

<QuickGrid Class="table" Items="@(!string.IsNullOrEmpty(TitleSearch) ? DB.Movie.Where(s => !string.IsNullOrEmpty(s.Title) ? s.Title.Contains(TitleSearch) : false) : DB.Movie)">

... but the ❓ stands on the no-op code below.

I'll place the full component code here for inspection ...

@page "/movies"
@using Microsoft.AspNetCore.Components.QuickGrid
@inject BlazorWebAppMovies.Data.BlazorWebAppMoviesContext DB
@using BlazorWebAppMovies.Models
@using Microsoft.EntityFrameworkCore

<PageTitle>Index</PageTitle>

<h1>Index</h1>

<p>
    <a href="movies/create">Create New</a>
</p>

<QuickGrid Class="table" Items="movies">
    <PropertyColumn Property="movie => movie.Title" />
    <PropertyColumn Property="movie => movie.ReleaseDate" Title="Release Date" />
    <PropertyColumn Property="movie => movie.Genre" />
    <PropertyColumn Property="movie => movie.Price" />

    <TemplateColumn Context="movie">
        <a href="@($"movies/edit?id={movie.Id}")">Edit</a> |
        <a href="@($"movies/details?id={movie.Id}")">Details</a> |
        <a href="@($"movies/delete?id={movie.Id}")">Delete</a>
    </TemplateColumn>
</QuickGrid>

@code {
    private DbSet<Movie>? movies;

    [SupplyParameterFromQuery]
    public string? TitleSearch { get; set; }

    protected override void OnParametersSet()
    {
        var movies = from m in DB.Movie
                     select m;

        if (!string.IsNullOrEmpty(TitleSearch))
        {
            movies = movies.Where(s => !string.IsNullOrEmpty(s.Title) ? s.Title.Contains(TitleSearch) : false);
        }
    }
}

It doesn't work with movies as an IQueryable either ❌ ...

private IQueryable<Movie>? movies;

Expected Behavior

The movies collection is valid for the Items param, and the movies are rendered by the QG.

Steps To Reproduce

☝️ Shown above.

Exceptions (if any)

None ... the QG merely displays no items.

.NET Version

8.0

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label May 28, 2024
@guardrex
Copy link
Contributor Author

Nevermind! The rubber 🦆 resolved this. Working code turned out to be ...

@code {
    private IQueryable<Movie>? movies;

    [SupplyParameterFromQuery]
    public string? TitleSearch { get; set; }

    protected override void OnParametersSet()
    {
        if (!string.IsNullOrEmpty(TitleSearch))
        {
            movies = DB.Movie.Where(s => !string.IsNullOrEmpty(s.Title) ? s.Title.Contains(TitleSearch) : false);
        }
        else
        {
            movies = DB.Movie;
        }
    }
}

... and we'll modify further on review of the PR later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

1 participant