4

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

1 Answer 1

4

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" 
         }
)  
Sign up to request clarification or add additional context in comments.

3 Comments

Suppose i need to disable the DropDownList instead of DropDownListFor means where i have to give the disabled= "disabled"
Hi, I want to disable/enable the dropdown for specific pages only based on the value,i am passing it through model. i tried passing true / false to the disabled but it's not working .can you help in this
@JasperManickaraj, For that, please visit this link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.