How to use C # enum in Javascript

I have an enumeration in C # code and I want to get the names from enum in my jQuery validation rules.

Enum:

public enum EnumItemField { Zero = 0, One = 1, Two = 2, Three = 3, Four = 4, Five = 5, } 

Validate:

 function updateFieldStatus() { } $(document).ready(function () { $("#IntegrationService").validate({ rules: { //Set range of Start "config.EnumFormat[Zero].Start": { required: true, digits: true, range: [1, 200] }, "config.EnumFormat[One].Start": { required: true, digits: true, range: [1, 200] }, }, submitHandler: function () { MsgBoxService.show({ //setId: "saveload", //objectId: "asrun" }); }, }); 

This really works, but I want to do something like this:

 "config.EnumFormat[" + item + "].Start": { required: true, digits: true, range: [1, 200] } 

In C #, I can get the names this way:

 foreach (var item in Enum.GetNames(typeof(EnumItemField))) 

How can I do this in javascript? Thanks for the advice!

+5
source share
1 answer

You can do the same as a JavaScript object.

 EnumItemField = { "Zero": 0, "One": 1, "Two": 2, "Three": 3, "Four": 4, "Five": 5 } 

Remember to remove the last comma,.

You can use the following values:

 EnumItemField.Zero // 0 EnumItemField["Zero"] // 0 

Iteration? No problems:

 for (var item in EnumItemField) { item; // Zero EnumItemField[item]; // 0 } 
+6
source

Source: https://habr.com/ru/post/1239433/


All Articles