Getting error in RenderFragment in a template grid component in ASP.NET BLAZOR Server

Viewed 292

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 Answers

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.

Related