Skip to content

Instantly share code, notes, and snippets.

@jltrem
Last active November 30, 2016 15:30
Show Gist options
  • Save jltrem/45928a76df226cfa94a1afce1764200b to your computer and use it in GitHub Desktop.
Save jltrem/45928a76df226cfa94a1afce1764200b to your computer and use it in GitHub Desktop.
Razor to autocreate JS dicts from C# enums
<!-- this should go in _Layout.cshtml -->
@{
string appInfo = MyAppNamespace.Util.MyAppInfo.ToJson();
<script>
window.myAppInfo = @Html.Raw(appInfo);
</script>
}
<!-- then load a js file with the following to extend this global object:
// extend the global application info object that was created by _Layout.cshtml
myAppInfo.getEnumFromName = function(list, name) {
var found = list.find(function(el) {
return el.Name === name;
});
return found ? found : undefined;
};
myAppInfo.getEnumFromVal = function(list, val) {
val = parseInt(val);
var found = list.find(function(el) {
return el.Val === val;
});
return found ? found : undefined;
};
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment