簡體   English   中英

對來自自定義 InputSelect 的 Blazor 驗證消息使用模型顯示名稱

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

正如我們所知,Blazor 不支持<InputSelect> Int 選項選擇(但支持字符串和枚舉),我們收到以下錯誤消息:

錯誤:System.InvalidOperationException:Microsoft.AspNetCore.Components.Forms.InputSelect 1[System.Nullable 1[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.";

如何顯示模型顯示名稱而不是 FieldName?

您的CustomInputSelect繼承自InputSelect InputSelect繼承自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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM