user2998990
user2998990

Reputation: 980

disable asp.net mvc dropdown using jquery

Below is my jquery code and my ASP>NET mvc razor code. Razor code is for dropdown list. I want to disable the dropdown when the labelCount variables value is 0. The values comes to 0 but the dropdown is not getting disabled.

if (labelCount == 0) {
    $("#AndOrSelection").attr("disabled", "disabled");
}

@Html.DropDownList("AndOrSelection", new List<SelectListItem>()
{ 
    new SelectListItem { Text = "And", Value = "And" },
    new SelectListItem { Text = "Or", Value = "Or" }
}, "Select criteria")

Upvotes: 1

Views: 3344

Answers (1)

Bellash
Bellash

Reputation: 8184

The best place to put your jQuery code is near the closing body tag (as best practices recommend)

Following will work

 @Html.DropDownList("AndOrSelection", new List<SelectListItem>()
 { 
   new SelectListItem { Text = "And", Value = "And" },
   new SelectListItem { Text = "Or", Value = "Or" }
 }, "Select criteria")

<script>
if (labelCount == 0) {
$("#AndOrSelection").attr("disabled", "disabled");
}
</script>
</body>

Upvotes: 1

Related Questions