.netblazorscaffolding

Is there a way to scaffold a model that has child classes inside?


I was following a blazor tutorial and there is this step where it can create a CRUD with just the model of a class. In the tutorial they only show it with a very simple class. I wanted to know if it is possible to do the same but with a class that has child class inside.

public class Match
{
    public int Id { get; set; }
    public DateTime MatchTimestamp { get; set; }
    public Player? Player1 { get; set; }
    public Player? Player2 { get; set; }
    public int Player1Score { get; set; }
    public int Player2Score { get; set; }
}

When I tried to scaffold this class, it did not completely generate the html.
Here an example in the Index.razor :

<QuickGrid Class="table" Items="context.Match">
    <PropertyColumn Property="match => match.MatchTimestamp" />
    <PropertyColumn Property="match => match.Player1Score" />
    <PropertyColumn Property="match => match.Player2Score" />

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

It did not generate the code to display Player1 or Player2. Is there a way to do it ?


Solution

  • if it is possible to do the same but with a class that as child class inside.

    No, scaffolding won't do that. I did a quick check in a MVC app and there scaffolding doesn't do navigation properties either. MVC has stabilized a long time ago.

    Is there a way to do it ?

    Not with scaffolding. The idea is to give you a quick start, nice for demos and proof of concept apps.

    In the real world you'll want to use services, repositories and ViewModels, implement soft-delete etc.

    So have a quick look at those scaffolded pages to see how they do it and then create your own template folder, one that you can copy/paste and rename to what you need. Blazors reliance on the file and folder names makes this relatively easy.