0

How to create a strongly type label for the enum dropdown. So I have one enum dropdown and I want to have a label for it but I want it to be strongly typed to the enum property. Is it not possible?

Model:

  public class CourseEnumsModel
    {
        public CourseEnums OptionSelected { get; set; }

        public string Error { get; set; }
    }

ENUMS:

   public enum CourseEnums
    {
        Java = 0,
        CSharp = 1,
        RubyOnRails = 2
    }

Views:

@model WebApplication3.Models.CourseEnumsModel
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<form method="post">
 // ONE STRONGLY TYPED LABEL HERE FOR THE DROPDOWN
   @Html.EnumDropDownListFor(model => model.OptionSelected)
   <input type="submit" value="Send" />
</form>

We have Display Label for the basic string properties

<dt>
    // I want something like this for the enum dropdown
    @Html.DisplayNameFor(model => model.AccountNumber)
</dt>

<dd>
    @Html.DisplayFor(model => model.AccountNumber)
</dd>
Unbreakable
  • 7,776
  • 24
  • 90
  • 171

1 Answers1

1

i think this should work

@Html.DisplayNameFor(model => model.OptionSelected )

public class CourseEnumsModel
    {
        DisplayName("<YOUR NAME>")
        public CourseEnums OptionSelected { get; set; }

        public string Error { get; set; }
    }
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • can you show me the output you are getting im sry im nt on my dev machine – Parv Sharma Jun 29 '17 at 21:04
  • Is there any difference between `//[DisplayName("")] [Display(Name ="")]`. Both seem to work – Unbreakable Jun 29 '17 at 21:10
  • [displayname attribute vs display attribute](https://stackoverflow.com/questions/5243665/displayname-attribute-vs-display-attribute) –  Jun 29 '17 at 22:08