C# 使用Blazor将输入文本动态绑定到类/对象属性

C# 使用Blazor将输入文本动态绑定到类/对象属性,c#,razor,data-binding,components,blazor,C#,Razor,Data Binding,Components,Blazor,我正在尝试使用Blazor为类内的属性构建一个输入字段的动态列表,但我不知道如何将输入框的内容绑定/链接到类的属性。(类have可以有大量公共道具,不仅仅是下面示例中的名称和描述,它们并不总是“string”类型) 假设我有这个类/模型: public class customer{ public string Name { get; set; } public int Age { get; set; } public string Descrip

我正在尝试使用Blazor为类内的属性构建一个输入字段的动态列表,但我不知道如何将输入框的内容绑定/链接到类的属性。(类have可以有大量公共道具,不仅仅是下面示例中的名称和描述,它们并不总是“string”类型)

假设我有这个类/模型:

public class customer{
        public string Name { get; set; }
        public int Age { get; set; }
        public string Description { get; set; }

}
我得到了这个blazor组件(updateC.razor):

假设这会将每个输入字段中所做的更改绑定到SCustomer当前实例中的正确属性(当输入时,它会更新类/对象的正确属性)。这不起作用,输入完成后,SCustomer内部的值不会更改。我猜我是完全错误的,但似乎不知道如何做到这一点,也找不到任何这样做的例子

@foreach(nfo中的var propertyInfo)
{
}

我终于找到了一种方法,下面是我的解决方案: 我创建了一个助手类:

public class PropHolder
    {
        [Parameter]
        public PropertyInfo info { get; set; }
        [Parameter]
        public string type { get; set; }
        public PropHolder(PropertyInfo nfo, string propType)
        {
            info = nfo;
            type = propType;
        }
    }
然后我创建了这个类的字典和一些检查函数(这在Clogic中)

[参数]
公共字典道具{get;set;}
公共无效GetAllProps()
{
Props=新字典();
//nfo=SCustomer.GetType().GetProperties();
int-Cid=0;
foreach(SCustomer.GetType().GetProperties()中的PropertyInfo pif)
{
道具[Cid]=新道具持有者(pif,pif.PropertyType.Name);
Cid++;
}
}
公共字符串校验名(PropHolder propertyInfo)
{
if(propertyInfo.GetType()==typeof(PropHolder))
{
返回属性info.type;
}
其他的
{
返回propertyInfo.GetType().Name.ToString();
}
}
公共财产信息获取信息(财产持有人财产信息)
{
if(propertyInfo.GetType()==typeof(PropHolder))
{
返回propertyInfo.info;
}
其他的
{
返回null;
}
}
最后,我能够循环我的字典的键并正确绑定所有值(从Answer中得到了很多帮助:“来自火星的agua”) 以下是updateC.razor的内容:

@if (Props != null)
        {

            @foreach (int key in Props.Keys)
            {

                var pinfo = Props[key];
                @if (CheckName(pinfo) == "String")
                {
                    <input type="text" class="form-control" value=@(getInfo(pinfo).GetValue(SCustomer)) @onchange="@((ChangeEventArgs __e) => getInfo(pinfo).SetValue(SCustomer, __e.Value.ToString()))" />
                }
            }
        }
@if(Props!=null)
{
@foreach(Props.Keys中的int键)
{
var pinfo=道具[键];
@如果(检查名称(pinfo)=“字符串”)
{
}
}
}
这给了我一个字符串类型的每个道具的输入框,如果要处理其他类型,现在可以很容易地添加它。这可能不是最好的解决方案,但确实有效。如果发布了更好的工作解决方案,我将更新Answare。

@foreach(nfo中的propertyInfo)
@foreach(propertyInfo in nfo)
{
  <label>@propertyInfo.Name</label>
  <input type="text" value="@propertyInfo.GetValue(SCustomer)" @onchange="@((ChangeEventArgs __e) => 
    propertyInfo.SetValue(SCustomer,Convert.ChangeType(__e.Value, 
    propertyInfo.PropertyType,null))"/>
}
{ @propertyInfo.Name }
这可能缺少结尾括号,并给出以下错误:“System.Reflection.TargetParameterCountException:“参数计数不匹配”。”值得注意的是,customer类的公共道具并不总是字符串类型。(我将尝试更新该问题,以便提及该问题)。类型为“text”的输入值始终为字符串。打开属性类型并根据类型创建其他类型的输入。我找到了一种方法,虽然有点难看,但这是我自己的答案。谢谢你的帮助!它引导了我很多。此外,如果你有一个更好的工作解决方案,比我更好的这个问题,请张贴,我一定会接受它!
[Parameter]
        public Dictionary<int, PropHolder> Props{ get; set; }
        public void GetAllProps()
        {
            Props = new Dictionary<int, PropHolder>();
            //nfo = SCustomer.GetType().GetProperties();
            int Cid = 0;
            foreach (PropertyInfo pif in SCustomer.GetType().GetProperties())
            {
                Props[Cid] = new PropHolder(pif, pif.PropertyType.Name);
                Cid++;
            }
        }

        public string CheckName(PropHolder propertyInfo)
        {
            if (propertyInfo.GetType() == typeof(PropHolder))
            {
                return propertyInfo.type;
            }
            else
            {
                return propertyInfo.GetType().Name.ToString();
            }

        }
        public PropertyInfo getInfo(PropHolder propertyInfo)
        {
            if (propertyInfo.GetType() == typeof(PropHolder))
            {
                return propertyInfo.info;
            }
            else
            {
                return null;
            }
        }
@if (Props != null)
        {

            @foreach (int key in Props.Keys)
            {

                var pinfo = Props[key];
                @if (CheckName(pinfo) == "String")
                {
                    <input type="text" class="form-control" value=@(getInfo(pinfo).GetValue(SCustomer)) @onchange="@((ChangeEventArgs __e) => getInfo(pinfo).SetValue(SCustomer, __e.Value.ToString()))" />
                }
            }
        }
@foreach(propertyInfo in nfo)
{
  <label>@propertyInfo.Name</label>
  <input type="text" value="@propertyInfo.GetValue(SCustomer)" @onchange="@((ChangeEventArgs __e) => 
    propertyInfo.SetValue(SCustomer,Convert.ChangeType(__e.Value, 
    propertyInfo.PropertyType,null))"/>
}