14 Mayıs 2018 Pazartesi

HtmlHelper DropDownList için Enumerations kullanımı

HtmlHelper DropDownList için Enumerations kullanımı, ASP.Net MVC ile birlikte kullanılan html helper çok işlevsel ve fonksiyonları çok yararlı. Eğer bir SelectList kullanıyorsanız bu projemizde işimizi dahada fazla görecektir. HtmlHelper DropDownList için Enumerations enum değerini görüntüler. Bu değeri görüntülemek için DisplayAttribute aracılığıyla ile değeri alabiliriz. Özellik mevcut değilse, o zaman enum değerinden ad alır.


HtmlHelper DropDownList için Enumerations kullanımı


 


private static readonly Dictionary<Enum, string> NameCache = new Dictionary<Enum, string>();
public static string GetName(this Enum type)

if (NameCache.ContainsKey(type))
return NameCache[type];

var enumType = type.GetType();
var info = enumType.GetField(type.ToString());
if (info == null)
return string.Empty;

var displayAttribute = info.GetCustomAttributes(false).OfType<DisplayAttribute>().FirstOrDefault();
var value = string.Empty;
if (displayAttribute != null)
value = displayAttribute.GetName() ?? string.Empty;

NameCache.Add(type, value);

return value;

Enum yardımı ile kullanma


public static MvcHtmlString DropDownListForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)

return htmlHelper.DropDownListForEnum(expression, null, null);

public static MvcHtmlString DropDownListForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)

return htmlHelper.DropDownListForEnum(expression, new RouteValueDictionary(htmlAttributes));

public static MvcHtmlString DropDownListForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)

return htmlHelper.DropDownListForEnum(expression, null, htmlAttributes);

public static MvcHtmlString DropDownListForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string optionLabel)

return htmlHelper.DropDownListForEnum(expression, optionLabel, null);

public static MvcHtmlString DropDownListForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string optionLabel, object htmlAttributes)

return htmlHelper.DropDownListForEnum(expression, optionLabel, null);

public static MvcHtmlString DropDownListForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string optionLabel, IDictionary<string, object> htmlAttributes)

if (expression == null)
throw new ArgumentNullException("expression");

var member = expression.Body as MemberExpression;
if (member == null)
throw new ArgumentNullException("expression");

var selectedValue = string.Empty;
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
if (metadata.Model != null)

selectedValue = metadata.Model.ToString();

var enumType = Nullable.GetUnderlyingType(member.Type) ?? member.Type;

var listItems = new List<SelectListItem>();
foreach (var name in Enum.GetNames(enumType))

var type = Enum.Parse(enumType, name) as Enum;
listItems.Add(new SelectListItem

Text = type.GetName(),
Value = name,
Selected = name == selectedValue
);


return htmlHelper.DropDownListFor(expression, listItems, optionLabel, htmlAttributes);


HtmlHelper DropDownList için Enumerations kullanımı

Hiç yorum yok:

Yorum Gönder