Reputation: 980
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
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