using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Linq.Expressions; using System.Text; using System.Web.Mvc.Html; namespace HeuristicLab.Services.Optimization.Web.Helpers { // http://stackoverflow.com/questions/5621013/pass-enum-to-html-radiobuttonfor-mvc3 public static class HtmlExtensions { public static MvcHtmlString RadioButtonForEnum( this HtmlHelper htmlHelper, Expression> expression ) { var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); var names = Enum.GetNames(metaData.ModelType); var sb = new StringBuilder(); foreach (var name in names) { var id = string.Format( "{0}_{1}_{2}", htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix, metaData.PropertyName, name ); var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString(); sb.AppendFormat( " {2}", id, HttpUtility.HtmlEncode(name), radio ); } return MvcHtmlString.Create(sb.ToString()); } } }