Note that there are some explanatory texts on larger screens.

plurals
  1. POpass enum to html.radiobuttonfor MVC3
    primarykey
    data
    text
    <p>I Have an Enum Called ActionStatus that has 2 possible values open=0 and closed = 1</p> <pre><code>public enum ActionStatus { Open, Closed } </code></pre> <p>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.</p> <p>Do i need an extension method for this, and has anybody already created one?</p> <p>EDIT: After Darins's answer below this is my Model class</p> <pre><code>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&lt;User&gt; Users { get; set; } public virtual ICollection&lt;ActionUpdate&gt; ActionUpdates { get; set; } public MeetingActionStatus ActionStatus { get; set; } } public enum MeetingActionStatus { Open, Closed } </code></pre> <p>and this is my view</p> <pre><code>@model Actioner.Models.MeetingAction @using Actioner.Helpers &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Description) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Description) @Html.ValidationMessageFor(model =&gt; model.Description) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.ReviewDate) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.ReviewDate) @Html.ValidationMessageFor(model =&gt; model.ReviewDate) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Status) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.RadioButtonForEnum(x =&gt; x.ActionStatus) &lt;/div&gt; </code></pre> <p>And this is my create controller action</p> <pre><code>public virtual ActionResult Create(int id) { var meetingAction = new MeetingAction { MeetingId = id, ReviewDate = DateTime.Now.AddDays(7) }; ViewBag.Title = "Create Action"; return View(meetingAction); } </code></pre> <p>The enum displays fine in the view, but the currently selected option is not persisted to the database</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload