简体   繁体   中英

Change IEnumerable dropdown to select enum value instead of number c#

I have a view model that is connected to an Ienumerable, and a view that is connected to the view model that pulls the Ienumerable as a drop down. My problem is when I select it, the value is 0,1,2,3 so on and so forth. How do I get it to pass the value of the Enum BRO,CGS,YHH,POH?

View

@Html.LabelFor(m => m.AllLicenseTypes)
@Html.DropDownListFor(m => m.AllLicenseTypes, new { @class = "form-control" })

ViewModel

[Display(Name = "License Types")]
public LicenseTypesEnum AllLicenseTypes { get; set; }

IEnumerable

public enum LicenseTypesEnum
{
    BRO,
    CGS,
    YHH,
    POH
}

First, enum != IEnumerable. They are only tangentially related.

Second, if you want to use that enum , what you're looking for is Enum.GetNames . Try changing your code to something like this:

[Display(Name = "License Types")]
public IEnumerable<string> AllLicenseTypes {
    get {
        return Enum.GetNames(typeof(LicenseTypesEnum));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM