简体   繁体   English

将字符串绑定值直接映射到 Blazor 组件中的 integer 属性

[英]Mapping string bind-Value directly to an integer property in Blazor component

I am developing a custom blazor select component.我正在开发自定义 blazor select 组件。 The component has a property named "Value" which in most cases is a string value which I can bind to using @bind-Value without any issues.该组件有一个名为“Value”的属性,在大多数情况下它是一个字符串值,我可以毫无问题地使用@bind-Value 绑定到它。

However in some situations the property which it binds to may be an integer (outside the component) in which case I need to convert from a string to an integer. I initially assumed simply using Int32.Parse would accomplish this as follows:但是,在某些情况下,它绑定的属性可能是 integer(在组件外部),在这种情况下,我需要将字符串转换为 integer。我最初假设只需使用 Int32.Parse 即可完成此操作,如下所示:

Select.razor.cs Select.razor.cs

namespace Accounting.Web.Components
{    
    public partial class Select
    {
        [Parameter]
        public string Value { get; set; } = default!;

        [Parameter]
        public EventCallback<string>? ValueChanged { get; set; }   

        public void OnClick()
        {
            Value = "2";
            ValueChanged?.InvokeAsync(Value);
        }
    }
}

Index.razor.cs Index.razor.cs

<Select Id="InvestmentEntitySelect" @bind-Value="@Int32.Parse(AddDto.InvestmentEntityId)">
    @foreach (var entity in Entities)
    {
        <SelectOption Value="@entity.InvestmentEntityId.ToString()">@entity.Name</SelectOption>
    }
</Select>   

AddDto.cs AddDto.cs

namespace Accounting.Shared.Dtos.Request.Investment
{
    public class AddDto
    {
        [Required]
        public int? InvestmentEntityId { get; set; } = 0;

        [Required]
        public string Description { get; set; } = String.Empty;
    }
}

The line of interest is:感兴趣的是:

@bind-Value="@Int32.Parse(AddDto.InvestmentEntityId)"

But unfortunately this produces an error:但不幸的是,这会产生一个错误:

Argument 1: cannot convert from 'int' to 'System.ReadOnlySpan'参数 1:无法从“int”转换为“System.ReadOnlySpan”

So how can I converted the bind value to an integer in the above example?那么在上面的示例中,如何将绑定值转换为 integer 呢?

You have to do it like this:你必须这样做:

<Select Id="InvestmentEntitySelect" Value="@AddDto.InvestmentEntityId.ToString()" ValueChanged="OnValueChanged">
    @foreach (var entity in Entities)
    {
        <SelectOption Value="@entity.InvestmentEntityId.ToString()">@entity.Name</SelectOption>
    }
</Select>

@code {
    private void OnValueChanged(string selectedValue)
    {
        AddDto.InvestmentEntityId = int.Parse(selectedValue);
    }
}

You need to create a custom input select like below and override the TryParseValueFromString method of it:您需要像下面这样创建一个自定义输入 select 并覆盖它的TryParseValueFromString方法:

public class CustomInputSelect<TValue> : InputSelect<TValue>
{
    protected override bool TryParseValueFromString(
        string? value,
        out TValue result,
        out string? validationErrorMessage)
    {
        if (typeof(TValue) == typeof(int))
        {
            if (int.TryParse(value, out var resultInt))
            {
                result = (TValue)(object)resultInt;
                validationErrorMessage = null;
                return true;
            }
            else
            {
                result = default;
                validationErrorMessage =
                    $"The selected value {value} is not a valid number.";
                return false;
            }
        }
        else
        {
            return base.TryParseValueFromString(value, out result,
                out validationErrorMessage);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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