DropDownList

PHOTO EMBED

Thu Aug 24 2023 05:09:18 GMT+0000 (Coordinated Universal Time)

Saved by @Ismaielov

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace CustomHelperMethod
{
    public static class HelperMethod
    {
        public static MvcHtmlString CustomGenderDropDownList(this HtmlHelper helper, string controlName,
                       string optionalLabel = "", object htmlAttributes = null)
        {

            IEnumerable<SelectListItem> selectList = null;
            List<SelectListItem> genderList = new List<SelectListItem>();
            genderList.Add(new SelectListItem() { Text = "Male", Value = "1" });
            genderList.Add(new SelectListItem() { Text = "Female", Value = "2" });
            selectList = (IEnumerable<SelectListItem>)new SelectList(genderList, "Value", "Text");
            if (htmlAttributes != null && htmlAttributes is RouteValueDictionary)
            {
                RouteValueDictionary htmlAttr = htmlAttributes as RouteValueDictionary;
                if (!string.IsNullOrEmpty(optionalLabel))
                    return helper.DropDownList(controlName, selectList, optionalLabel, htmlAttr);
                else
                    return helper.DropDownList(controlName, selectList, htmlAttr);
            }
            if (!string.IsNullOrEmpty(optionalLabel))
                return helper.DropDownList(controlName, selectList, optionalLabel, htmlAttributes);
            else
                return helper.DropDownList(controlName, selectList, htmlAttributes);
        }
    }
}
content_copyCOPY

https://helpfordeveloper.blogspot.com/search/label/ASP.NET MVC