Error in enum c # when placing arithmetic sign

0

I am filling a ComboBox through an enum in MVC c #, in that combo are filled as options of the arithmetic operators combo, but I only require the symbol (+, -, *, /), when I only place the sign generates me an error

I have already placed it in double and single quotes but it still marks me that an identifier was expected, there is also another way placing the tag [Description (...)] but I could not binde this tag, the way I binded the Combo is:

@Html.DropDownList("DDLOperadores", EnumHelper.GetSelectList(typeof(EnumOperadores.MuestraOperadores)), "Selecciona el Tipo", new { @class = "form-control col-md-12" })
    
asked by Ivxn 18.06.2018 в 19:55
source

2 answers

0

enum do not accept values in quotes.

To do this, use the decorator [Description("...")] to add a custom value. Then use the following method to read the value of the decorator.

public static string GetDescription(this Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

    if (attributes != null && attributes.Length > 0)
    {
        return attributes[0].Description;
    }
    else
    {
        return value.ToString();
    }
}
    
answered by 18.06.2018 / 21:06
source
0

Hello basically an enumerated is a naming convention for integer types. Unable to assign string type values to them. If you want to use something similar to a list that returns strings you should do something like that

public static class MyOperators
{
    public const string Less = "<"; 
    public const string LessOrEqual = "<=";
    public const string Greater = ">";
    public const string GreaterOrEqual = ">=";
    public const string Add = "+";
    public const string Minus = "-";
}
    
answered by 18.06.2018 в 21:05