Bind Drop Down by Enums in mvc/ asp.net

First of all you create a enum calss and define enum like FontFamily
 
 
public static partial class Enums
    {
      public enum FontFamily
        {
            /// <summary>
            /// The undefined
            /// </summary>
            Undefined,
 
            /// <summary>
            /// The courier
            /// </summary>
            Courier,
 
            /// <summary>
            /// The helvetica
            /// </summary>
            Helvetica,
 
            /// <summary>
            /// The times new roman
            /// </summary>
            TimesNewRoman,
 
            /// <summary>
            /// The symbol
            /// </summary>
            Symbol
        }
}
 
C# Code convert FontFamily Enums in list type and convert into SelectListItem 
 
            List<SelectListItem> fontList = new List<SelectListItem>();
            var fontLists = Enum.GetValues(typeof(Enums.FontFamily));
            for (int i = 0; i < fontLists.Length; i++)
            {
                fontList.Add(new SelectListItem { Text = ((Enums.FontFamily[])fontLists)[i].ToString(), Value = ((Enums.FontFamily[])fontLists)[i].ToString() });
             } 
 

Comments

Popular posts from this blog

Form submit resulting in “InvalidDataException: Form value count limit 1024 exceeded.” In ASP.NET Core

Calculate Dynamic Current Calendar / Financial Year In C#

Repository Design Pattern in C#