0

Possible Duplicate:
MVC3 Razor DropDownListFor Enums

I'm a newbie in ASP.NET MVC3 (REALLY NEW). I want to create a Drop Down List Box for four year levels of students. I have: "First", "Second", "Third", and "Fourth". Now for that in model I have created an Enum named YLevels as follows:

  public enum YLevels
    {
      First =1,
      Second,
      Third ,
      Fourth
    }

As a whole, my model class StudentMT contains:

 public StudentMT()
    {
        Remarks = string.Empty;
    }

    public int Id { get; set; }

    [Required(ErrorMessage = "First Name is required.")]
    [StringLength(30, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
    [Display(Name= "First Name")]
    public string FName { get; set; }

    [Required(ErrorMessage = "Last Name is required.")]
    [StringLength(30, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
    [Display(Name = "Last Name")]
    public string LName { get; set; }

    public string Gender { get; set; }

    [Display(Name="Year Level")]
    public int YLevel { get; set; }

    public string Remarks { get; set; }

    public enum YLevels
    {
        First =1,
        Second,
        Third ,
        Fourth
    }
  }

then in my view, i want to use an EditorFor():

    <div class="editor-label">
        @Html.LabelFor(model => model.YLevel)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.YLevel)
        @Html.ValidationMessageFor(model => model.YLevel)<br/> 
    </div>

how can i define a property that would use this enum? How can I display the dropdown list using the EditorFor()? Please suggest what I can possibly do.

Thank you in advance!

Community
  • 1
  • 1
ideAvi
  • 139
  • 2
  • 3
  • 13
  • @DarinDimitrov: thanksfor that, but is there any way for me to create a dropdownlist using @Html.EditorFor() instead of @Html.DropDownListFor()? – ideAvi Jan 20 '12 at 07:07
  • yes, there is: you could define a custom editor template for the given property and inside this custom editor template use the custom helper shown in the post I have linked to. – Darin Dimitrov Jan 20 '12 at 07:11

1 Answers1

0

Could you try :

[Display(Name="Year Level")]
public YLevels YLevel { get; set; }
Narayan Akhade
  • 2,694
  • 3
  • 21
  • 30
  • thanks. I tried but it displays a textbox, not a dropdownlist – ideAvi Jan 20 '12 at 09:56
  • @Html.DropDownListFor(m=>m.YourModelProperty,new SelectList(Enum.GetValues(typeof(YourEnumType)))) this one liner should get it done – JM1990 Nov 07 '14 at 10:50