Search code examples
razorparametersnestedblazor

Pass Value in Nested Parameter in Blazor Component


How do i pass value in nested parameter. suppose i have a custom control called mycomponent

mycomponent.Razor

<label>
    
</label>
@code
{
   [Parameter]
    public TestBase Test { get; set; } = new TestBase();
}

TestBase Class

public class TestBase
{
    [Parameter]
    public string Cap { get; set; }
    [Parameter]
    public string Cap5 { get; set; }="hai"


    [Parameter]
    public string Cap2 { get; set; }


    [Parameter]
    public string Cap3 { get; set; }

}

MyPage

<mycomponent Test.Cap="my value">

</mycomponent>

Test.Cap="my value" is not working

What is the right way to pass value in nested Parameter


Solution

  • You should be passing TestBase into MyComponent, not trying to set a value of TestBase in MyComponent. Setting a default value for Test in MyComponent just covers the situation where the Parameter is not provided.

    <mycomponent Test="MyTest">
    
    </mycomponent>
    
    @code {
    
      private TestBase MyTest = new TestBase() {Cap = "Test Value"};
    
      SomeButtonClick Event() 
      {
        MyTest.Value = "Another Value";
      }
    }
    

    I suggest you read up about components - This is a good start or MS Docs.

    Update

    straight answer to your question is: No you can't do what you're trying to do.