0

I am working with MVC 5 application, and have Enum like this.

public enum Status
{
        [Description("Submitted")]
        Submitted=1,

        [Description("Approved")]
        Approved=2,

        [Description("Rejected")]
        Rejected=3
}

Now want to bind it with the DropDownList directly(means don't want to go from any C# code, directly in @Html.DropDownList).

How it could be possible?

I am using Enum helper class also. Please guide me.

Jeric Cruz
  • 1,899
  • 1
  • 14
  • 29
iDipa
  • 347
  • 2
  • 9
  • 20
  • Html.DropDownListFor(o => o.EnumProperty, Enum.GetValues(typeof(enumtype)).Cast().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() })) here enum type is your Status enum and EnumProperty is your class model property that you are want to bind with dropdown – Parth Savadiya Apr 05 '18 at 17:39
  • Possible duplicate of [How do you create a dropdownlist from an enum in ASP.NET MVC?](https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc) – Parth Savadiya Apr 05 '18 at 17:43
  • It giving me error like " 'System.Array' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)" I have write this kind of syntex : ` @Html.DropDownList("Status", Enum.GetValues(typeof(Status)).Cast().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() })) ` – iDipa Apr 05 '18 at 17:48
  • 1
    try this @Html.DropDownList("Status", new SelectList(Enum.GetValues(typeof(Status))), " Select", new { @class = "form-control" }) – Parth Savadiya Apr 05 '18 at 17:58
  • It solve dropdown binding issue but now have another issue. Its not giving text-value combination in dropdown. dropdown not showing value. – iDipa Apr 05 '18 at 18:19

0 Answers0