CSharp · Mvc
using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Reflection;
namespace HelloWorldMvcApp
{
public class SampleViewModel {
public int Selected { get; set; }
public List<SelectListItem> SelectedList { get; set; }
}
public enum YogaTime
{
[Display(Name = "5:00 AM")]
Five,
[Display(Name = "5:15 AM")]
FiveFifteen,
[Display(Name = "5:30 AM")]
FiveThirty,
[Display(Name = "5:45 AM")]
FiveFortyFive,
[Display(Name = "6:00 AM")]
Six,
[Display(Name = "7:00 AM")]
Seven,
[Display(Name = "8:00 AM")]
Eight,
[Display(Name = "9:00 AM")]
Nine,
[Display(Name = "10:00 AM")]
Ten,
[Display(Name = "11:00 AM")]
Eleven,
[Display(Name = "12:00 PM")]
Twelve,
[Display(Name = "1:00 PM")]
Thirteen,
[Display(Name = "2:00 PM")]
Fourteen,
[Display(Name = "3:00 PM")]
Fifteen,
[Display(Name = "4:00 PM")]
Sixteen,
[Display(Name = "5:00 PM")]
Seventeen,
[Display(Name = "6:00 PM")]
Eighteen,
[Display(Name = "7:00 PM")]
Nineteen,
[Display(Name = "8:00 PM")]
Twenty
}
public static class Helpers {
// taken from /a/32193206
public static string GetDisplayName(this Enum enumeration)
{
Type enumType = enumeration.GetType();
string enumName = Enum.GetName(enumType, enumeration);
string displayName = enumName;
try
{
MemberInfo member = enumType.GetMember(enumName)[0];
object[] attributes = member.GetCustomAttributes(typeof(DisplayAttribute), false);
DisplayAttribute attribute = (DisplayAttribute)attributes[0];
displayName = attribute.Name;
if (attribute.ResourceType != null)
{
displayName = attribute.GetName();
}
}
catch { }
return displayName;
}
}
}
@model HelloWorldMvcApp.SampleViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- CSS Includes -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<style type="text/css">
.field-validation-error {
color: #ff0000;
}
</style>
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h1>Hello Stranger</h1>
@using (Html.BeginForm())
{
<div class="form-group">
@Html.DropDownListFor(m => m.Selected, Model.SelectedList)
</div>
}
<br/><br/>
<div class="alert alert-warning fade">
<img src="http://entechprod.blob.core.windows.net/dotnetfiddle/morpheus.jpg" style="max-width:100%;"/><br/><br/>
<strong><span class="alert-content"></span></strong>
</div>
</div>
</div>
<!-- JS includes -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
var model = new SampleViewModel();
var selectlist = Enum.GetValues(typeof(YogaTime))
.Cast<YogaTime>().Where(x => x >= YogaTime.Fourteen)
.Select(x => new SelectListItem() {
Value = ((int)x).ToString(),
Text = ((Enum)(object)x).GetDisplayName()
});
model.SelectedList = selectlist.ToList();
return View(model);
}
}
}