Here a page, myPage.razor
@page "/myPage"
<div class="row">
<div class="col">
<ComponentA />
</div>
</div>
In the ComponentA I use a ComponentB
<div class="row">
<div class="col">
<ComponentB />
</div>
</div>
In the myPage.razor.cs there is an instance of a class Person. I'd like pass this instance to ComponentA and to ComponentB.
I tried with [Parameter] and [CascadingParameter] without any success.
Is there a way to do this ?
Thanks,
Update 1: The Microsoft sample below works but that's not do what I want (below the child code). Microsoft
@page "/parameter-parent"
<h1>Child component (without attribute values)</h1>
<ParameterChild />
<h1>Child component (with attribute values)</h1>
<ParameterChild Title="Set by Parent" Body="@(panelBody)" />
@code {
PanelBody panelBody = new PanelBody() { Text = "Set by parent.", Style = "italic" };
}
//Child
@using Testing.Pages
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" style="font-style:@Body.Style">
@Body.Text
</div>
</div>
@code {
[Parameter]
public string Title { get; set; } = "Set By Child";
[Parameter]
public PanelBody Body { get; set; } =
new()
{
Text = "Set by child.",
Style = "normal"
};
}
But I have to receive the object (not create a new one), display in an <input type="text"/> be able to change the text. The new value is update in the object in the ParameterParent. I tried the code below but Body is null all the time (the parent is the same then code above), I tried CascadingParameter too
@using Testing.Pages
<div class="card w-25" style="margin-bottom:15px">
<div class="card-header font-weight-bold">@Title</div>
<div class="card-body" >
@Body.Text
</div>
</div>
<InputText id="name" @bind-Value="Body.Text" />
@code {
[Parameter]
public string Title { get; set; } = "Set By Child";
[Parameter]
public PanelBody Body { get; set; }
}
Parameterworks. Post what you actually tried. The code you posted has no parameterswithout success. What did you try and what didn't work? Have you checked the docs on component parameters? - add[Parameter]to the property you want to expose inComponentBand use that as an attribute, eg<ComponentB MyProperty=@_whatever />[Parameter], not some internal black-box mechanism. I've been using component parameters for two years. People can't guess what's wrong with code you didn't post. I'd bet you didn't add the parameter inComponentB. Or you tried to use a string literal instead of@(_someField)to pass the value