2

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 ?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried adding a PropertyColumn that accesses the child class properties?

In the code below, I added match.Player1?.Name and match.Player2?.Name , you could change this to whatever property your Player class has.

<QuickGrid Class="table" Items="context.Match">
    <PropertyColumn Property="match => match.MatchTimestamp" />
    <PropertyColumn Property="match => match.Player1?.Name" />
    <PropertyColumn Property="match => match.Player1Score" />
    <PropertyColumn Property="match => match.Player2?.Name" />
    <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>

1 Comment

Yes, that will work as long as the Players are Included. But the question is about generating these columns.
-1

A "database-first" tool CA.ApiGenerator PowerShell module is designed for this exact scenario.

It has built-in templates that are smart enough to:

  1. Read your database schema, including all relationships.

  2. Automatically generate the DTOs (ViewModels) for you.

  3. Generate the complete "real-world" solution (Clean Architecture, CQRS, API controllers) to serve that data, correctly handling all the navigational properties and other code that the default scaffolder misses.

DISCLOSURE: I am the author of CA.ApiGenerator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.