Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable a 'DropDownListFor' on a checkbox selection

I am working show the drop down values on selection of a check box. But with the below code, all different items that are present in the drop down coming directly.

Somehow the @Html.DropDownListFor is not working inside select tag, can anyone please help me to fix it?

Note: Checkbox toggle working perfectly.

Thank you,

.cshtml

<input type="checkbox" /> 
<p>check this box to escalate</p>          
<select disabled="disabled">    
@Html.DropDownListFor(
    model => model.EscalationQueue,
        new SelectList(Extensions.EscalationQueue, Model.EscalationQueue),
          new { @class = "escalation-queue", name = Model.EscalationQueue }
)  
</select>

.js file

var dropdownToggle = function () {
    $("input:checkbox").change(function () {
        if ($("input:checkbox").is(":checked")) {
            $("select").removeAttr("disabled");
        }
        else {
            $("select").attr("disabled", "disabled");
        }
    });
}

$(document).ready(function () {
    dropdownToggle();
});

Output:

CurrentOutput1

CurrentOutput2

like image 924
ary Avatar asked Oct 21 '25 15:10

ary


1 Answers

You dont need the <select> tag, the HtmlHelper DropDownListFor will render that for you. If you want to initialize the dropdown as disabled set the html attribute as you did for declaring a cssclass...

<input type="checkbox" /> 
<p>check this box to escalate</p>  
@Html.DropDownListFor(
        model => model.EscalationQueue,
        new SelectList(Extensions.EscalationQueue, Model.EscalationQueue),
        new { @class = "escalation-queue", 
                name = Model.EscalationQueue, 
                disabled= "disabled" 
         }
)  
like image 111
Gabe Avatar answered Oct 23 '25 08:10

Gabe