# Component Association

### Parent Component

For `.razor` file component, you should create cascading component manually

{% code title="List.razor" %}

```cshtml
@inherits BlazorComponentBase

<CascadingValue Value="this">
    <ul @attributes="AdditionalAttributes">@ChildContent</ul>
</CascadingValue>
```

{% endcode %}

For component class, just define `ParentComponent` attribute

```csharp
[ParentComponent]
public class List : BlazorComponentBase
{
}
```

### Child Component

Define `ChildComponent` attribute for child component

{% code title="ListItem.razor" %}

```cshtml
@inherits BlazorComponentBase
@attribute [ChildComponent(typeof(List))]

<!--For .NET 7 can use generic attribute-->
@attribute [ChildComponent<List>]

<li @attributes="@AdditionalAttributes">
    @ChildContent
</li>

@code{
    [CascadingParameter]public List CascadedList { get; set; }
    
    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        if(CascadedList.Active)
        {
            //..
        }
    }
}
```

{% endcode %}

{% code title="ListItem.cs" %}

```csharp
[ChildComponent(typeof(List))]
//[ChidlComponent<List>] // for .net7
public class ListItem : BlazorComponentBase
{
    [CascadingParameter]public List CascadedList { get; set; }
    
    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        if(CascadedList.Active)
        {
            //..
        }
    }
}
```

{% endcode %}

So `ListItem` component must be inside of `List` component, or an exception will be thrown if `ListItem` dose not inside of `List` component

```cshtml
<List>
    <ListItem>...</ListItem>
</List>

<!--throw exception-->
<ListItem />
```

### Optional

Set `true` ignore the exception if not in parent component

{% code title="ListItem.razor" %}

```cshtml
@inherits BlazorComponentBase
@attribute [ChildComponent(typeof(List))]

<!--For .NET 7 can use generic attribute-->
@attribute [ChildComponent<List>]

<li @attributes="@AdditionalAttributes">
    @ChildContent
</li>

@code{
    [CascadingParameter]public List CascadedList { get; set; }
    
    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        if(CascadedList.Active)
        {
            //..
        }
    }
}
```

{% endcode %}

{% code title="ListItem.cs" %}

```csharp
[ChildComponent(typeof(List), Optional = true)]
//[ChidlComponent<List>(Optional = true)] // for .net7
public class ListItem : BlazorComponentBase
{
    //This instance may be null
    [CascadingParameter]public List? CascadedList { get; set; }
    
    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        if(CascadedList is not null && CascadedList.Active)
        {
            //..
        }
    }
}
```

{% endcode %}

```cshtml
<!--never throw exception-->
<ListItem />
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://playermaker.gitbook.io/componentbuilder/english/component-association.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
