public static IList <SelectListItem> GetEnumList <T>() { return(EnumHelper.GetSelectList(typeof(T))); }
public static MvcHtmlString EnumDropDownListFor <TModel, TEnum>( this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TEnum> > expression, string optionLabel, IDictionary <string, object> htmlAttributes ) { if (expression == null) { throw Error.ArgumentNull("expression"); } ModelMetadata metadata = ModelMetadata.FromLambdaExpression( expression, htmlHelper.ViewData ); if (metadata == null) { throw Error.Argument( "expression", MvcResources.SelectExtensions_InvalidExpressionParameterNoMetadata, expression.ToString() ); } if (metadata.ModelType == null) { throw Error.Argument( "expression", MvcResources.SelectExtensions_InvalidExpressionParameterNoModelType, expression.ToString() ); } if (!EnumHelper.IsValidForEnumHelper(metadata.ModelType)) { string formatString; if (EnumHelper.HasFlags(metadata.ModelType)) { formatString = MvcResources.SelectExtensions_InvalidExpressionParameterTypeHasFlags; } else { formatString = MvcResources.SelectExtensions_InvalidExpressionParameterType; } throw Error.Argument( "expression", formatString, metadata.ModelType.FullName, "Flags" ); } // Run through same processing as SelectInternal() to determine selected value and ensure it is included // in the select list. string expressionName = ExpressionHelper.GetExpressionText(expression); string expressionFullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionName); Enum currentValue = null; if (!String.IsNullOrEmpty(expressionFullName)) { currentValue = htmlHelper.GetModelStateValue(expressionFullName, metadata.ModelType) as Enum; } if (currentValue == null && !String.IsNullOrEmpty(expressionName)) { // Ignore any select list (enumerable with this name) in the view data currentValue = htmlHelper.ViewData.Eval(expressionName) as Enum; } if (currentValue == null) { currentValue = metadata.Model as Enum; } IList <SelectListItem> selectList = EnumHelper.GetSelectList( metadata.ModelType, currentValue ); if ( !String.IsNullOrEmpty(optionLabel) && selectList.Count != 0 && String.IsNullOrEmpty(selectList[0].Text) ) { // Were given an optionLabel and the select list has a blank initial slot. Combine. selectList[0].Text = optionLabel; // Use the option label just once; don't pass it down the lower-level helpers. optionLabel = null; } return(DropDownListHelper( htmlHelper, metadata, expressionName, selectList, optionLabel, htmlAttributes )); }