Converting Enum Values to Generic List

Working with Enum values is very useful in so many circumstances and when you need to covert these enum values to List<SelectListItem>, here is how you can do it;


public List<SelectListItem> GetPaymentOptions()
{
    PaymentOptions[] values = (PaymentOptions[])Enum.GetValues(typeof(PaymentOptions));

    var list = from value in values
               select new SelectListItem()
               {
                   Value = ((int)value).ToString(),
                   Text = value.ToString()
                };

    List<SelectListItem> paymentOptions = list.ToList<SelectListItem>();

    return paymentOptions;
}

That is all- nice and simple!

Happy coding!

Comments

Popular posts from this blog

Solution for "Can not obtain Oracle client information from registry"

Solution for the fault: "System.Diagnostics.Process.Start is not working on IIS, but working on ASP.NET Development Server"