How to display an EditorFor based on a selected item from a DropDownList in MVC 5?

0

Greetings I have the following situation.

I have the following code in a view

<div class="form-group">
    @Html.LabelFor(model => model.IdDestinoPersona, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("IdDestinoPersona", null, "SELECCIONE UN DESTINO", htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.IdDestinoPersona, "", new { @class = "text-danger" })
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Destino, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Destino, new { htmlAttributes = new { @class = "form-control",  id = "destinoAlternativo" } })
            @Html.ValidationMessageFor(model => model.Destino, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

I just need the EditorFor to be activated when the DropDownList selects the option "SELECT A DESTINATION" and when a real destination of the DropDownList is selected, the EditorFor will be deactivated.

Could you help me solve this situation? Is there any way to do it with JQuery?

    
asked by Galaviz Arroyo 09.07.2018 в 18:52
source

1 answer

0

This script should help. The idea is to capture the 'change' event and add or remove the 'disable' attribute according to the action.

<script>
    $(document).ready(function () {
        $('#IdDestinoPersona').on('change', function () {
            if ($('#IdDestinoPersona').val() == '') {
                alert('remove');
                $('#destinoAlternativo').removeAttr("disabled");
            }
            else {
                alert('add');
                $('#destinoAlternativo').attr("disabled", true);
            }
        });
    });
</script>
    
answered by 10.07.2018 / 02:53
source