Created
April 4, 2012 19:04
-
-
Save ferventcoder/2304804 to your computer and use it in GitHub Desktop.
MVC Html DropDownListHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EnumerationExtensions | |
{ | |
public static IEnumerable<SelectListItem> GetEnumerationItems(this Enum enumeration) | |
{ | |
var listItems = Enum | |
.GetValues(enumeration.GetType()) | |
.OfType<Enum>() | |
.Select(e => | |
new SelectListItem() | |
{ | |
Text = e.GetDescriptionOrValue(), | |
Value = e.ToString(), | |
Selected = e.Equals(enumeration) | |
}); | |
return listItems; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.DataAnnotations; | |
using System.Web.Mvc; | |
namespace SomeMvcProject.Web.Models | |
{ | |
public class RegistrationModel : IModel | |
{ | |
[Required] | |
[Display(Name = "Position")] | |
public PositionType Position { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PositionType | |
{ | |
[Description("Shop Owner")] | |
ShopOwner, | |
Member, | |
Associate | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="editor-label"> | |
@Html.LabelFor(model => model.Position) | |
</div> | |
<div class="editor-field"> | |
@Html.DropDownListFor(m => m.Position, Model.Position.GetEnumerationItems()) | |
@Html.ValidationMessageFor(model => model.Position) | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment