在 Blazor 组件的嵌套参数中传递值

Pass Value in Nested Parameter in Blazor Component

如何在嵌套参数中传递值。假设我有一个名为 mycomponent

的自定义控件

mycomponent.Razor

<label>
    
</label>
@code
{
   [Parameter]
    public TestBase Test { get; set; } = new 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; }

}

我的页面

<mycomponent Test.Cap="my value">

</mycomponent>

Test.Cap="我的值" 无效

在嵌套参数中传递值的正确方法是什么

您应该将 TestBase 传递给 MyComponent,而不是试图在 MyComponent 中设置值 TestBase。在MyComponent中设置Test的默认值只是覆盖了没有提供Parameter的情况。

<mycomponent Test="MyTest">

</mycomponent>

@code {

  private TestBase MyTest = new TestBase() {Cap = "Test Value"};

  SomeButtonClick Event() 
  {
    MyTest.Value = "Another Value";
  }
}

我建议您阅读有关组件的内容 - This is a good start or MS Docs

更新

对你的问题的直接回答是:不,你不能做你想做的事。