简体   繁体   English

对来自自定义 InputSelect 的 Blazor 验证消息使用模型显示名称

[英]Use Model Display name for Blazor Validation message from Custom InputSelect

As we know Blazor doesn't support Int option select for <InputSelect> (but support string & Enum) and we get the following error message:正如我们所知,Blazor 不支持<InputSelect> Int 选项选择(但支持字符串和枚举),我们收到以下错误消息:

Error: System.InvalidOperationException: Microsoft.AspNetCore.Components.Forms.InputSelect 1[System.Nullable 1[System.Int32]] does not support the type 'System.Nullable`1[System.Int32]'.错误:System.InvalidOperationException:Microsoft.AspNetCore.Components.Forms.InputSelect 1[System.Nullable 1[System.Int32]] 不支持类型“System.Nullable`1[System.Int32]”。

Therefore, I write a <CustomInputSelect> :因此,我写了一个<CustomInputSelect>

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 {FieldIdentifier.FieldName} field is Required.";
                    return false;
                }
            }
            else
            {
                return base.TryParseValueFromString(value, out result, out validationErrorMessage);
            }
        }
    }

I have the following Model:我有以下型号:

public class Risk : ICloneable
    {
        /// <summary>
        /// Associated Legal Entity of the risk
        /// </summary>
        [Required]
        [Display(Name = "Legal Entity")]
        public int? LegalEntityId { get; set; }

        /// <summary>
        /// Associated Legal Entity Short Code of the risk
        /// </summary>
        [Required]
        [Display(Name = "Legal Entity Short Code")]
        public string LegalEntityShortCode { get; set; }
}

The following Blazor page:以下 Blazor 页面:

    <CustomInputSelect id="ddlLegalEntity" class="form-control InputTextHeigth" @bind-Value="ShowRisk.LegalEntityId">
            @foreach (var option in LEList)
            {
                <option value="@option.Id">
                 @option.OptionName
                </option>
            }
    </CustomInputSelect>
<div class="cs-col-6">
    <ValidationMessage class="form-control" For="@(() => ShowRisk.LegalEntityId)" />
</div>

Everything is working fine.一切正常。 I can use int value on Option list.我可以在选项列表上使用 int 值。 However, The validation error is generating as Field name instead of Display name.但是,验证错误生成为字段名称而不是显示名称。 So I getting "The LegalEntityId field is Required."所以我得到“ LegalEntityId字段是必需的”。 instead of "The Legal Entity field is Required".而不是“法人实体字段是必需的”。

From the first snapshot of the code, the message is generated:从代码的第一个快照生成消息:

validationErrorMessage = $"The {FieldIdentifier.FieldName} field is Required.";

How can I display Model Display name instead of FieldName?如何显示模型显示名称而不是 FieldName?

Your CustomInputSelect inherits from InputSelect .您的CustomInputSelect继承自InputSelect InputSelect inherits from InputBase . InputSelect继承自InputBase This abstract class has a property called DisplayName .这个抽象类有一个名为DisplayName的属性。

It should be filled by either setting it manually in the razor file or via a Display attribute on your view model.它应该通过在 razor 文件中手动设置或通过视图模型上的Display属性来填充。

<InputDate @bind-Value="@moveOutViewModel.MoveOutDate" DisplayName="@localize["moveout_step_date_legend"]" />

or或者

[Display(Name = "address_street", ResourceType = typeof(Translations))]
public string Street { get; set; }

In your custom select class, you can use the following code:在您的自定义选择类中,您可以使用以下代码:

if (int.TryParse(value, out var resultInt))
{
    result = (TValue)(object)resultInt;
    validationErrorMessage = null;
    return true;
}
else
{
    result = default;
    validationErrorMessage = $"The '{this.DisplayName ?? this.FieldIdentifier.FieldName}' field is required.";
    return false;
}

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

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