简体   繁体   中英

How to bind input value to object property in Blazor

I have a object property like this: public object Data { get; set; } public object Data { get; set; }

And I have some input with different types in .razor component:

switch (DataType)
{
    case DataType.Boolean:
        <input @bind="Data" type="checkbox" />
        break;
    case DataType.String:
        <input @bind="Data" type="text" />
        break;
    case DataType.Number:
        <input @bind="Data" type="number" />
        break;
}

Аs a result I get error:

System.Object 类型没有支持从字符串转换的关联 TypeConverter

how can I bind these input to object property?

I had a similar problem and using @onchange instead of @bind resolved it.

switch (DataType)
{
    case DataType.Boolean:
        <input @onchange="@(x => { Data = x; })" type="checkbox" />
        break;
    case DataType.String:
        <input @onchange="@(x => { Data = x; })" type="text" />
        break;
    case DataType.Number:
        <input @onchange="@(x => { Data = x; })" type="number" />
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM