0

I am using Asp.net Core MVC. I have a property that has the data annotation of required.

property:

    [Required(ErrorMessage = "O campo SEXO é obrigatório")]
     public SEXO Sexo { get; set; }

enum:

     public enum SEXO {
        MASCULINO = 0,
        FEMININO = 1
    }

select element

            <select asp-for="Sexo" class="form-control">
                <option value="">SELECIONE</option>
                <option value="0">Homem</option>
                <option value="1">Mulher</option>
            </select>

When I enter some text in a text input field and erase it the required message appears. But when I select an option in a select element that has a value that is no considered empty like:

 <option value="0">Homem</option>

And afterwards change it back to

<option value="">SELECIONE</option>

The required message doesn't appear. This make the user experience very uneven. Is there some easy way to fix this or I will have to fire some Javascript.

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
  • since i dont really know how to do what your asking.. my suggestion would be, if it's required, don't include a "not selected item" in the list.. this might help as well https://stackoverflow.com/questions/5805059/how-do-i-make-a-placeholder-for-a-select-box – JamieD77 Mar 01 '18 at 20:17
  • 1
    MVC's client side validation using `jquery.validate` **will** display an error message if the selected option is `SELECIONE` assuming you have included the element with `asp-validation-for="Sexo"`. If its not working for you, then its due to something you have not shown us –  Mar 01 '18 at 20:55

0 Answers0