1

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")
3
  • Your code works fine. Is the script in a document.ready() block? Are you getting any errors in the browser console? Commented Mar 13, 2015 at 11:32
  • 1
    @StephenMuecke : thanks.. the code wasn't inside document.ready(). Thanks .. Commented Mar 13, 2015 at 11:34
  • 1
    You can also simplify you code a bit - @Html.DropDownList("AndOrSelection", new SelectList(new List<string>() { "And", "Or" }), "Select criteria") Commented Mar 13, 2015 at 11:36

1 Answer 1

1

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

Comments

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.