[英]How to generate dynamically blazor's @bind-Value?
我在玩 Blazor,我想创建一个动态生成的表单,它是网格的一部分。 数据的实体类型被传递到网格,这是表单的基础。 我现在面临的问题是@bind-Value
,这是能够编辑给定实体所必需的。
我使用BlazorStrap ,它是一个 Blazor 服务器端项目。
我在剃刀文件中有以下代码:
@{
string bind = $"{nameof(_editedItem)}.{propertyInfo.Name}";
}
<BSBasicInput InputType="InputType.Text"
id="@propertyInfo.Name"
@bind-Value="@bind"/>
有问题的部分是最后一行。 始终显示_variableName.PropertyName
而不是从对象中提取值。
正确的代码应如下所示:
<BSBasicInput InputType="InputType.Text"
id="@propertyInfo.Name"
@bind-Value="_variableName.PropertyName"/>
到目前为止我尝试过的:
@bind-Value
值这里的解决方案是什么?
#nullable restore
#line 24 "/../..ModalEdit.razor"
bind
#line default
#line hidden
#nullable disable
, 31, Microsoft.AspNetCore.Components.EventCallback.Factory.Create(this, Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this,
__value => bind = __value, bind)));
__builder4.AddMarkupContent(32, "\n");
您无法绑定,但您可以使用回调和 lambda 函数来存档相同的结果:
@if (user != null)
{
@foreach (var property in user.GetType().GetProperties())
{
<Field>
<FieldLabel>@(property.Name): @(property.PropertyType.FullName)</FieldLabel>
@switch (property.PropertyType.FullName)@*Chose editing style*@
{
case "System.String":
<TextEdit Text="@property.GetValue(user)?.ToString()" TextChanged="value => property.SetValue(user, value)" />
break;
case "System.Boolean":
<Switch TValue="bool" Checked="(bool)property.GetValue(user)" CheckedChanged="value => property.SetValue(user, value)" />
break;
default: /*Unsuported/Not implemented editing*/
<TextEdit Text="@property.GetValue(user)?.ToString()" Disabled="true" />
break;
}
</Field>
}
}
我在本例中使用 Blazorise 控件,但您可以使用任何控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.