开发者

ASP.NET MVC3 DropDownListFor Enum

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
{
    pub开发者_开发技巧lic 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?


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)


You can also change your model class using DayOfWeek:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜