I Have an Enum Called ActionStatus that has 2 possible values open=0 and closed = 1
public enum ActionStatus { Open, Closed }
I want to build radio button group in my edit and create views that uses the enum to populate the radio buttons with a) a default value in the create view and b) the currently selected option in the edit view.
Do i need an extension method for this, and has anybody already created one?
EDIT: After Darins's answer below this is my Model class
namespace Actioner.Models { [MetadataType(typeof(MeetingActionValidation))] public class MeetingAction { [Key] public int MeetingActionId { get; set; } [Required] [Display(Name = "Description")] public string Description { get; set; } [Required] [Display(Name = "Review Date")] public DateTime ReviewDate { get ;set; } public int Status{ get; set; } [ScaffoldColumn(false)] public int MeetingId { get; set; } //public virtual Meeting Meeting { get; set; } //public virtual ICollection Users { get; set; } public virtual ICollection ActionUpdates { get; set; } public MeetingActionStatus ActionStatus { get; set; } } public enum MeetingActionStatus { Open, Closed }
public virtual ActionResult Create(int id) { var meetingAction = new MeetingAction { MeetingId = id, ReviewDate = DateTime.Now.AddDays(7) }; ViewBag.Title = "Create Action"; return View(meetingAction); }
The enum displays fine in the view, but the currently selected option is not persisted to the database
回答1:
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace YourNamespace { 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()); } } }
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 description = name; var memInfo = metaData.ModelType.GetMember(name); if (memInfo != null) { var attributes = memInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false); if (attributes != null && attributes.Length > 0 ) description = ((DisplayAttribute)attributes[0]).Name; } 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(description), radio ); } return MvcHtmlString.Create(sb.ToString()); } }