1

I'm designing a reusable grid component in blazor with following model:

public class ProductDetails
{
    public List<productDetails> productDetails { get; set; }
}

public class productDetails
{
    public int id { get; set; }
    public string productName { get; set; }
    public List<tiresAndRates> tiresAndRates { get; set; }
}

public class tiresAndRates
{
    public string quantity { get; set; }
    public decimal price { get; set; }
    public string type { get; set; }
}

I've this TableTemplate.razor file:

@typeparam TItem

<table class="table">
    <thead>
        <tr>@TableHeader</tr>
    </thead>
    <tbody>
        @foreach (var item in Items)
        {
            <tr>@RowTemplate(item)</tr>
        }
    </tbody>
</table>

@code {
    [Parameter]
    public RenderFragment TableHeader { get; set; }

    [Parameter]
    public RenderFragment<TItem> RowTemplate { get; set; }

    [Parameter]
    public IReadOnlyList<TItem> Items { get; set; }
}

but when I bind the data from parent component then I'm getting an error saying :

Severity Code Description Project File Line Suppression State Error CS0411 The type arguments for method 'TypeInference.CreateTableTemplate_0(RenderTreeBuilder, int, int, IReadOnlyList, int, RenderFragment, int, RenderFragment)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

and the component where I'm using the template:

<TableTemplate Items="productLists" Context="productDetails">
    <TableHeader>
        <th>ID</th>
        <th>Product</th>
    </TableHeader>
    <RowTemplate>
        <td>@productDetails.id</td>
        <td>@productDetails.productName</td>
    </RowTemplate>
</TableTemplate>

    public ProductDetails productLists { get; set; } = new ProductDetails();
    public AuditReport auditReports { get; set; } = new AuditReport();

    protected override async Task OnInitializedAsync()
    {
        string filePath = @"D:\ProductDetails.json";
        productLists = JsonConvert.DeserializeObject<ProductDetails>(File.ReadAllText(filePath));
        string filePath1 = @"D:AuditReport.json";
        auditReports = JsonConvert.DeserializeObject<AuditReport>(File.ReadAllText(filePath1));
    }
}

1 Answer 1

3

This paramater is a list:

public IReadOnlyList<TItem> Items { get; set; } 

Your not passing a list.

Items="productLists"

...
public ProductDetails productLists { get; set; } = new ProductDetails();

I think your after:

Items="productLists.productDetails"

I would recommend that you read up on C# coding standards/conventions. Class and property names etc.

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

2 Comments

Thanks @Brian, could you please share any content which can help me in designing such screen where inside a reusable grid row ,a link can be appended and click on the same can open a dialog where I can update some values.
@LogicalDesk your above code is a HTML Table .. A CSS grid is a better choice IMHO. If the browser supports Blazor it most likely supports grids.

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.