1

Hi I'm fairly new to MVC3 and I'm trying to do something that I think must be fairly common, but can't quite get it.

I have a model I wish store the value for enum DayOfWeek in:

public class Booking
{
    public int ID { get; set; }
    public int Day { get; set; }
    ....
}

I've made it an int to store in the database.

I want to edit in the View as a DropDownList:

    <div class="editor-field">
        @Html.DropDownListFor(model => model.Booking.Day, new SelectList(Enum.GetValues(typeof(DayOfWeek))))
        @Html.ValidationMessageFor(model => model.Booking.Day)
    </div>

However I get the error: "The field day must be a number".

I know I'm missing something, and probably something simple, can anyone help?

John McArthur
  • 916
  • 1
  • 12
  • 30

2 Answers2

3

Add a SelectList property to your viewmodel and populate as per @Brandon's answer here.

Then you will change your code to:

@Html.DropDownListFor(model => model.Booking.Day, Model.SelectListProperty)

(where SelectListProperty is the name of your property on your viewmodel)

Community
  • 1
  • 1
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
0

You can also change your model class using DayOfWeek:

public class Booking
{
    public int ID { get; set; }
    public DayOfWeek Day { get; set; }
}
Massimo Zerbini
  • 3,125
  • 22
  • 22