Last active
November 30, 2016 15:30
-
-
Save jltrem/45928a76df226cfa94a1afce1764200b to your computer and use it in GitHub Desktop.
Razor to autocreate JS dicts from C# enums
This file contains 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
<!-- 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