Create a gist now

Instantly share code, notes, and snippets.

private void ProvideSelectListForEnums(ModelMetadata modelMetadata, Type modelType)
{
if (modelType != null && modelType.IsEnum && string.IsNullOrEmpty(modelMetadata.TemplateHint))
{
modelMetadata.TemplateHint = "SelectList";
var values = Enum.GetValues(modelType).Cast<object>();
var items = values.Select(entry => new SelectListItem { Text = Enum.GetName(modelType, entry), Value = entry.ToString() });
var selectList = new SelectList(items, "Value", "Text", modelMetadata.Model);
modelMetadata.AdditionalValues.Add("SelectList", selectList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment