HtmlHelper for displaying Enum values

If you like to use a lot of enums in you code this is a nice HtmlHelper to display these enum values in a MVC view dropdownbox

        public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, TEnum selectedValue)
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))

            int teller = 0;
            List<SelectListItem> itemsList = new List<SelectListItem>();
            foreach (var item in values)
		        itemsList.Add(new SelectListItem
                    Text = item.ToString(),
                    Value = teller.ToString(),
                    Selected = (item.Equals(selectedValue))
            IEnumerable<SelectListItem> selectlistitems = itemsList.AsEnumerable<SelectListItem>();

            MvcHtmlString totot = htmlHelper.DropDownList(
                new { @class = "DropDownDomeinExtensies" }
            var tempstring = totot.ToString();
            while (tempstring.Contains("#test#"))
               tempstring = tempstring.Replace("#test#", name);
            return MvcHtmlString.Create(tempstring);

Now you can easyli use it like this:

        <div class="editor-label">
            @Html.LabelFor(model => model.Type)
        <div class="editor-field">
            @(Html.EnumDropDownList<Uwwebburo.Models.AdresType>("Type", Model.Type))
            @Html.ValidationMessageFor(model => model.Type)




Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!