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

What is the importance of EDMX file in Entity Framework

TRIGGER in sql server

Sending Email in asp.net or mvc using gmail or other smpt.