【问题标题】:Use Model Display name for Blazor Validation message from Custom InputSelect对来自自定义 InputSelect 的 Blazor 验证消息使用模型显示名称
【发布时间】:2021-02-06 14:47:23
【问题描述】:

我们知道 Blazor 不支持为 <InputSelect> 选择 Int 选项(但支持字符串和枚举),我们收到以下错误消息:

错误:System.InvalidOperationException:Microsoft.AspNetCore.Components.Forms.InputSelect1[System.Nullable1[System.Int32]] 不支持类型“System.Nullable`1[System.Int32]”。

因此,我写了一个<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);
            }
        }
    }

我有以下型号:

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; }
}

以下 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>

一切正常。我可以在选项列表中使用 int 值。但是,验证错误生成为字段名称而不是显示名称。 所以我得到“LegalEntityId 字段是必需的。”而不是“法律实体字段是必填项”。

从代码的第一个快照,生成消息:

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

如何显示模型显示名称而不是字段名称?

【问题讨论】:

标签: c# razor blazor blazor-server-side


【解决方案1】:

您的CustomInputSelect 继承自InputSelectInputSelect 继承自 InputBase。 这个抽象类有一个名为DisplayName的属性。

应该通过在 razor 文件中手动设置或通过视图模型上的 Display 属性来填充它。

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

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

在您的自定义选择类中,您可以使用以下代码:

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;
}

【讨论】:

  • 嗨比约恩,感谢您的回复。你从哪里得到“输入”变量?看起来您的解决方案非常接近,但无法真正工作如何获取此输入变量??
  • @Shuvra 我已经更改了代码。原始代码是一种扩展方法,因此使用了输入。当您从 InputSelect 继承时,您可以使用 this.DisplayName 或简单地使用 DisplayName
  • 在 InputBase 中看不到 DisplayName 属性。我的组装:组装 Microsoft.AspNetCore.Components.Web,版本 = 3.1.6.0。我使用的是旧版本吗
  • @Shuvra 我明白了。我们使用的是“Microsoft.AspNetCore.Components.Forms”包的 5.0.3 版。但是,要使用此版本,您需要定位 .Net 5
  • 谢谢你,伙计。你的回答很有用。
猜你喜欢
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 2014-08-08
  • 2017-09-30
  • 1970-01-01
相关资源
最近更新 更多