Activate an input field according to an enum

2

I have an enum of schooling if in case the user select the level of schooling as incomplete Superior or complete I want to appear a field to enter what is the course

How can I do this?

    <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            @Html.LabelFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EnumDropDownListFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.GrauEscolaridade, "", new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>
                </div>



   public enum GrauEscolaridade{
        [Display(Name = "Superior incompleto ")]
        SuperiorIncompleto = 15,
        [Display(Name = "Superior Completo")]
        SuperiorCompleto = 16
}
    
asked by anonymous 05.06.2017 / 05:39

1 answer

2

Make a change to your dropdownListFor: Added "@id = 'my_name_dropDownList'"

<div class="panel panel-default">
                    <div class="panel-body">
                        <div class="form-group">
                            @Html.LabelFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EnumDropDownListFor(model => model.GrauEscolaridade, htmlAttributes: new { @class = "form-control", @id = "nome_do_meu_dropDownList" })
                                @Html.ValidationMessageFor(model => model.GrauEscolaridade, "", new { @class = "text-danger" })
                            </div>
                        </div>
                    </div>
                </div>

Placing at the end of your view (Html)

<input type="text" name="NomeCurso">


// Referência ao framework jquery CDN
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scripttype="text/javascript">

$(document).ready(function() {

    $('#NomeCurso').hide();

    $("#nome_do_meu_dropDownList").change(function () {

       // 15 e 16 são o Id do Superior completo ou superior incompleto
       if ($("#nome_do_meu_dropDownList").val() == '15' || $("#nome_do_meu_dropDownList").val() == '16') {
           $('#NomeCurso').show(); // Exibe o campo text para digitar o nome do curso
       } else {
           $('#NomeCurso').hide(); // Oculta o campo nome do curso
       }

   });

});

</script>
    
05.06.2017 / 21:05