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

Cant pass renderFragment to component #41

Open
Amir0715 opened this issue Nov 21, 2022 · 0 comments
Open

Cant pass renderFragment to component #41

Amir0715 opened this issue Nov 21, 2022 · 0 comments

Comments

@Amir0715
Copy link

Hi, I'm learning from your website. I have reached the lesson https://blazor-university.com/templating-components-with-renderfragements/passing-placeholders-to-renderfragments/
And I ran into the problem that I can't pass the template to the DataListTemplate parameter of the RenderFragment<RendetFragment> type of the DataList component. I get the following error during the build:

D:\projects\MS.CRM.Clients\MS.CRM.BlazorWebAssembly\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Pages_Debug_razor.g.cs(532,283,532,293): error CS0234: The type or namespace name 'AspNetCore' does not exist in the namespace '__Blazor.Microsoft' (are you missing an assembly reference?)

DataList.razor:

@typeparam T

@if (DataListTemplate == null)
{
  <ul>
    @foreach (var item in Data ?? Array.Empty<T>())
    {
      @ItemTemplate(item)
    }
  </ul>
}
else
{
  @DataListTemplate(
    @:@{
      foreach (var item in Data ?? Array.Empty<T>())
      {
        ItemTemplate(item)
      }
    }
    )
}

@code {

    [Parameter]
    public IEnumerable<T> Data { get; set; } = null!;

    [Parameter]
    public RenderFragment<T> ItemTemplate { get; set; } = null!;

    [Parameter]
    public RenderFragment<RenderFragment>? DataListTemplate { get; set; } = item => @item;

    protected override void OnInitialized()
    {
        if (Data == null) throw new ArgumentNullException(nameof(Data));
        if (ItemTemplate == null) throw new ArgumentNullException(nameof(ItemTemplate));
        base.OnInitialized();
    }
}

How i use:

@page "/debug"

<div class="container">
    <DataList Data="@Persons">
        <DataListTemplate Context="Data">
            <table>
                <thead>
                <tr>
                    <th>Salutation</th>
                    <th>Given name</th>
                    <th>Family name</th>
                </tr>
                </thead>
                <tbody>
                    @Data
                </tbody>
            </table>
        </DataListTemplate>
        <ItemTemplate>
            <li @key="context">@context.Name (Age: @context.Age)</li>
            @* <li>@context</li> *@
        </ItemTemplate>
    </DataList>
</div>

@code {

    public class Person
    {
        public Person(string Name, int Age)
        {
            this.Name = Name;
            this.Age = Age;
        }

        public string Name { get; init; }
        public int Age { get; init; }
    }

    IEnumerable<Person> Persons;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Persons = new[]
        {
            new Person("Alex", 10),
            new Person("Lecy", 20),
            new Person("Suzi", 15),
        };
    }

}

Pls, can you help me?
I also get error hint in my ide for Wig-Pig syntax:
WigPig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant