2

As we know Blazor doesn't support Int option select for <InputSelect> (but support string & Enum) and we get the following error message:

Error: System.InvalidOperationException: Microsoft.AspNetCore.Components.Forms.InputSelect1[System.Nullable1[System.Int32]] does not support the type 'System.Nullable`1[System.Int32]'.

Therefore, I write a <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:

    <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. However, The validation error is generating as Field name instead of Display name. So I getting "The LegalEntityId field is Required." 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?

1

1 Answer 1

3

Your CustomInputSelect inherits from InputSelect. InputSelect inherits from InputBase. This abstract class has a property called DisplayName.

It should be filled by either setting it manually in the razor file or via a Display attribute on your view model.

<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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Bjorn, Thanks for the reply. Where you get the 'input' variable from? Looks like your solution is very close but can't really work how can I get this input variable??
@Shuvra I've changed the code. The original code was an extension method, hence the use of input. You can use this.DisplayName or simply DisplayName when you inherit from InputSelect.
Can't see the DisplayName property in InputBase. My Assembley: Assembly Microsoft.AspNetCore.Components.Web, Version=3.1.6.0. am I using an older version
@Shuvra I see. We are using version 5.0.3 of the "Microsoft.AspNetCore.Components.Forms" package. However, to use this version you need to target .Net 5.
thank you mate. you answer was very useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.