0

Currently I have a dropdown list based on an enum in a viewmodel. The enum in the viewmodel looks like this:

public enum Frequency
{
    [Display(Name = "Daily")]
    Daily,
    [Display(Name = "Weekly")]
    Weekly,
    [Display(Name = "Monthly")]
    Monthly
}

In my razor view, I create the dropdown like this:

@Html.EnumDropDownListFor(model => model.Frequency, "--- Select frequency for assignment ---", new { @class = "form-control", id = "frequency" })

And the model gets referenced in the razor view like this:

@model MyApplication.ViewModel.MyViewModel

This works great. However, I want to make a dropdown select list for this instead. I have seen lots of posts that do this with a select list you create in either the controller or the view itself, but I would like to use the enum that comes from the viewmodel. Is this possible? Is it possible to do this without editing the controller?

Any help/guidance is greatly appreciated. Thanks.

Update

I was able to get this working based on How do you create a dropdownlist from an enum in ASP.NET MVC? (Suggested in a response). I changed the view like this:

Html.DropDownList("Frequency", EnumHelper.GetSelectList(typeof(MyApplication.ViewModel.Frequency)), "--Select frequency--", new { @class = "form-control", id = "frequency" })

Thanks!

Community
  • 1
  • 1
JOEMan90
  • 3
  • 4
  • 2
    Did you see [How do you create a dropdownlist from an enum in ASP.NET MVC?](http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) – Shyju Aug 23 '16 at 23:08
  • Wow, totally did not find that link. Using the information there, I was definitely able to get this working. Thank you! Will update answer with final solution. – JOEMan90 Aug 23 '16 at 23:40

0 Answers0