【问题标题】:any way to expand an Enum in Razor to simplify this Javascript code?有什么方法可以在 Razor 中扩展枚举以简化此 Javascript 代码?
【发布时间】:2014-05-14 18:31:11
【问题描述】:

我有以下 Javascript 代码块,其中包括手动获取枚举的值和文本,并将它们放入名为 source 的数组中:

$('#configuration').editable({ 
    url: '/UTS/UnitSave', 
    pk: '@Model.unit.c_number',
    prepend: "Unknown",
    source: [
    {
        value: @UnitStatus.Phases.Unknown,
        text: 'Unknown'
    },
    {
        value: @UnitStatus.Phases.Manufacturing,
        text: 'Manufacturing'
    },
    {
        value: @UnitStatus.Phases.Shipping,
        text: 'Shipping'
    },
    {
        value: @UnitStatus.Phases.Deployed,
        text: 'Deployed'
    }
    ]
});

这工作正常,但如果我要向Phases 添加另一个值,我需要记得回到我的View 并修改上面的代码块。有什么方法可以让这个更健壮,并简化代码来扩展枚举UnitStatus.Phases

【问题讨论】:

    标签: javascript asp.net razor enums


    【解决方案1】:

    我们在这里看不到 Phases 的样子,但如果这只是你想添加到这个 Object 中的东西,为什么不考虑

    function generatePhases(Phases) {
        var arr = [], key, has;
        has = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
        for (key in Phases)
            if (has(Phases, key))
                arr.push({
                    value: Phases[key],
                    text: key
                });
        return arr;
    }
    
    // and usage
    var o = { 
        url: '/UTS/UnitSave', 
        pk: '@Model.unit.c_number',
        prepend: "Unknown",
        source: generatePhases(@UnitStatus.Phases);
    };
    $('#configuration').editable(o);
    

    binding callhasOwnProperty 的奇怪之处在于让您安全地使用 hasOwnProperty,而不用假设它没有被 Object 遮蔽。

    【讨论】:

      【解决方案2】:

      考虑到你的枚举是一个 c# 枚举,你可以这样做。

      namespace webapitest
      {
          public enum UnitStatusPhases
              {
                  Unknown,
                  Manufacturing,
                  Shipping,
                  Deployed
              }
      
              public class utility
              {
                 public static string SomeMethod(System.Type enumType)
                  {
                      List<MyEnumDescriptor> list = new List<MyEnumDescriptor>();
      
                      foreach (Enum enumValue in Enum.GetValues(enumType))
                      {
                          list.Add(new MyEnumDescriptor(enumValue.ToString(), enumValue.ToString()));
                      }
                      return JsonConvert.SerializeObject(list);
                  }
      
                  class MyEnumDescriptor
                  {
                      public MyEnumDescriptor(string value, string text)
                      {
                          this.Value = value;
                          this.Text = text;
                      }
                      public string Value { get; set; }
                      public string Text { get; set; }
                  }
              }
      }
      

      对于 json 序列化,我使用的是 NewtonsoftJs

      在你看来,你可以写。

      @using webapitest;
      <div id="body">
          <section class="featured">
      ...
      </section>
      </div>
      
      <script type="text/javascript">
          alert('just a alert');
          var value = JSON.parse('@Html.Raw(utility.SomeMethod(typeof(UnitStatusPhases)))');
          console.log(value);
      </script>
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 2019-08-01
        • 2011-02-08
        • 1970-01-01
        • 2010-09-21
        • 2014-03-31
        • 1970-01-01
        相关资源
        最近更新 更多