The application has enum, which is part of the model and its extension is impossible. (Type created solely for example)

public enum Cars : int { Nissan = 1, Kia, Toyota, BMW } 

In View there is a dropDown. In addition to the existing values, it is necessary that the dropDown also contain at least the "All" and "None" records, but others are possible. This is necessary for filtering. You need to understand that the value selected in dropDown-e will be sent back to the server, and will be compared with the property of type enum Cars for existing domain objects. What is the best way to do this?

  • And what should come to the server if All is selected? Tell me more about what kind of logic you are coding. - VladD
  • @VladD any int value. For example, you can encode All = 99, None = 100. It does not matter. You can offer your option. - user181892
  • So why would these values ​​All and None not add to enum? - null
  • @null Cannot change the original enum. - user181892
  • @Suneption: What should the server do if the All value comes to it? Is he ready for this turn of events? - VladD

1 answer 1

Is it acceptable to create a new listing in this way?

 public enum Cars : int { Nissan = 1, Kia, Toyota, BMW } public enum CarsExtension: int { None = -1, All = 999, Nissan = Cars.Nissan, Kia = Cars.Kia, Toyota = Cars.Toyota, BMW = Cars.BMW } 
  • Is it dangerous. Imagine that one more constant was added to Cars , then CarsExtension will be wrong, and this will not be understood when compiled. - VladD
  • > You can not change the original enum // suitable if the original enum cannot be changed - SanŚ́́́́́́́́́́́́́́́
  • I understood "it is impossible" as "does not belong to us." But the owners of this enum can easily change it. Well, changing the model object at will / whim of the display level code is somehow completely wrong. - VladD
  • I agree, but what to do, I proposed an option that the author may not have considered, besides the obvious ones: don’t transfer anything when choosing None and check all values ​​when choosing All, i.e. a little js or checkboxlist / multiselect, and on the server to accept the list of selected options). + The acceptability of use is completely unclear. Let the author of the question decide. - SanŚ́́́́́́́́́́́́́́́́́́́́́́́́-
  • those. such a list for sure was considered jsfiddle.net/1moqhpu7 (without js it was only presented and it was as clear as it should work), and on the server side it was taken in the action string [] selectedValues - SanŚ́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́