问题描述
我正在与Blazor一起玩,我想创建一个动态生成的表单,该表单是网格的一部分.数据的实体类型传递到网格,这是表单的基础.我现在面临的问题是 @ bind-Value
,这是能够编辑给定实体所必需的.
I'm playing with Blazor and I'd like to create a dynamically generated form, which is part of a grid.The entity type of the data is passed to the grid and this is the base of the form. The problem I'm facing now is @bind-Value
, which is needed to be able to edit the given entity.
我使用 BlazorStrap ,它是Blazor服务器端项目.
I use BlazorStrap, and it is a Blazor Server Side project.
我在剃刀文件中有以下代码:
I have the following code in the razor file:
@{
string bind = $"{nameof(_editedItem)}.{propertyInfo.Name}";
}
<BSBasicInput InputType="InputType.Text"
id="@propertyInfo.Name"
@bind-Value="@bind"/>
有问题的部分是最后一行.始终显示 _variableName.PropertyName
,而不是从对象中拉出值.
The problematic part is the last line. Always the _variableName.PropertyName
is displayed instead of pulling out the value from the object.
正确的代码应如下所示:
The proper code should look like this:
<BSBasicInput InputType="InputType.Text"
id="@propertyInfo.Name"
@bind-Value="_variableName.PropertyName"/>
到目前为止我尝试过的事情:
What I tried so far:
- 语法的多种风格,但是,由于我对Razor语法没有真正的经验,所以在碰壁之后
- 调试表明我要编辑的可变变量获得了其值,因此问题出在生成的
@ bind-Value
值 - 我还检查了生成的代码(.g文件),但在那里没有发现任何臭味(请在下面找到)
- 如果没有动态生成的表单,我还没有创建POC,因为创建类似的东西要花几个小时.
这里的解决方案是什么?
What is the solution here?
#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函数来归档相同的结果:
you cant bind, but you can use callbacks and lambda functions to archive the same results:
@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控件,但是您可以使用任何控件.
I'm using Blazorise controls in this example, but you can use any.
这篇关于如何动态生成blazor的@ bind-Value?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!