Skip to content

Instantly share code, notes, and snippets.

@timhobbs
Created October 22, 2011 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timhobbs/1305873 to your computer and use it in GitHub Desktop.
Save timhobbs/1305873 to your computer and use it in GitHub Desktop.
Razor dynamic drop down helper
@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