Create a gist now

Instantly share code, notes, and snippets.

Embed
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