Created
October 22, 2011 10:56
-
-
Save timhobbs/1305873 to your computer and use it in GitHub Desktop.
Razor dynamic drop down helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@helper DynamicDropDowns() { | |
<script type="text/javascript"> | |
function listChanged($list, $target, url) { | |
var listId = $list.val(); | |
if (listId == "") { // User selected first option, so clear and disable the list | |
$target.empty(); | |
enableList($target, false); | |
return; | |
} | |
$.getJSON(url, { id: listId }, function (data) { | |
$target.empty(); // Clear the list | |
$.each(data, function (idx, item) { // Add the data | |
$target.append($("<option/>", | |
{ | |
value: item.Value, | |
text: item.Text | |
})); | |
}); | |
enableList($target, true); // Enable the list | |
}); | |
} | |
function enableList($list, enabled) { | |
$list.attr("disabled", enabled ? null : "disabled"); | |
} | |
</script> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment