0

I have a enum and I am trying to model bind it using ASP.NET MVC.

public class MyViewModel
{
  public enum ESampleTime
    {
        Value1= 0,
        Value2,
        Value3,
        Value4,
        Value5,
        Value6
    }
}

And I am trying to model bind it using ,

<div class="form-group">
            @Html.LabelFor(model => model.SampleTime, htmlAttributes: new { @class="control-label col-md-2"})
            <div class="col-md-2">
                @Html.DropDownListFor(model => model.SampleTime,new List<SelectListItem>())
            </div>

This results in empty list as I am passing a new SelectedItem List.

But how do I display Value1, value2... in listBoix?

Simsons
  • 12,295
  • 42
  • 153
  • 269
  • Are you want to pass enum into `ListBoxFor`? Seems you may look at these: https://stackoverflow.com/questions/26862025/passing-enum-values-to-a-listboxfor-in-asp-net-mvc3 & https://stackoverflow.com/questions/19143126/asp-net-mvc-model-binding-with-listboxfor-dropdownlistfor-helpers. – Tetsuya Yamamoto Jan 19 '18 at 08:11
  • Is this a bad idea ? I just want to constrain user selection and not really need a listbox to bind. But as I am at design phase want to go with best practice. – Simsons Jan 19 '18 at 08:15
  • You just need to add a List of "SelectListItem" to you model. Pre-populate the list in your controller and bind to the list in the drop down. – Wheels73 Jan 19 '18 at 08:16

0 Answers0