【发布时间】:2022-09-27 15:40:19
【问题描述】:
我正在尝试使用 .net 6 中发布的新动态组件功能。我已经观看了 youtube 上的所有视频并完成了示例。我似乎根本不知道如何从组件中获取值。我使用了与事件绑定在一起的动态组件的参数属性,但我的用例是将一堆动态组件加载到页面上,并且提交按钮是父页面的一部分,而不是动态组件。单击提交时,我只想要父页面上文本框动态组件的值。这是一个例子:
文本框组件
@Label: <input type=\"text\" style=\"margin: 5px;\" @bind-value=\"@TextBoxValue\"/>
@code{
public string Label { get; set; }
public string TextBoxValue { get;set; }
protected override Task OnInitializedAsync()
{
return base.OnInitializedAsync();
}
}
索引页:
@page \"/\"
@if (type != null)
{
<DynamicComponent Type=\"type\" />
}
<button class=\"btn btn-primary\" @onclick=\"SaveToDatabase\">Submit</button>
@code {
Type type;
protected async override Task OnInitializedAsync()
{
type = typeof(TextBoxComponent);
}
private void SaveToDatabase()
{
// get the text value of the dynamic component and insert into db
}
}
我尝试创建一个名为 Appstate 的对象并分配一个字符串属性,但我仍然无法获得该值。