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?

4
  • What is the value of mode.Booking.Day at that point in the code? Is it a number? or is it null? or something else? Commented Jun 24, 2011 at 10:11
  • As far as I can tell, it's the string of the enum. ie: Monday Commented Jun 24, 2011 at 10:36
  • Isn't model.Booking.Day an int? "public int Day { get; set; }" Commented Jun 24, 2011 at 10:51
  • Yes, and that's why I'm getting the error. I want to choose the day from the drop down and return the enum value, rather than the string. Commented Jun 24, 2011 at 11:15

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

0

You can also change your model class using DayOfWeek:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.