Search code examples
c#.net-coreerror-messaging

How can I change the default model error "The value '' is invalid."?


I want to change the default error text of a DropDown field. Unfortunately I just can't get it to work.

I have entered my own ErrorMessage in the controller, but it does not work.

[Required(ErrorMessage = "Please select a country")]
public int OfficesCountryId { get; set; }

View-part:

                        <div class="form-group">
                            <label asp-for="OfficesCountryId" class="col-md-6 control-label">Country:</label>
                            <div class="col-md-5">
                                <select asp-for="OfficesCountryId" asp-items="Html.GetEnumSelectList<Offices.OfficesCountryNumbers>().OrderBy(n => n.Text)">
                                    <option value="">@SharedLocalizer["Bitte wählen"]</option>
                                </select>
                            </div>
                            <div class="col-md-10">
                                <span asp-validation-for="OfficesCountryId" class="text-danger"></span>
                            </div>
                        </div>

enter image description here

Thanks for a hint / solution


Solution

  • try this

     <option value="0">@SharedLocalizer["Bitte wählen"]</option>
    

    and maybe change your data validation attribute

     [Range(1, 200, ErrorMessage = "Please select a country")]
    public int OfficesCountryId { get; set; }