ASP.NET实体框架 - DropDownListFor枚举值

时间:2016-09-13 17:20:33

标签: c# asp.net enums

我有一个类似的课程:

namespace CentralParkVIPPreview.Models
{
    [Table("CP-VIP-Preview")]
    public class CP_VIP_Preview
    {
        [Key]
        [DisplayName("Occupancy Timeline")]
        [Required]
        public string occupancyTimeline { get; set; }
    }

    public class Homefront : DbContext
    {
        public DbSet<CP_VIP_Preview> Data { get; set; }
    }

    public enum occupancyTimeline : int
    {
        TwelveMonths = 12,
        FourteenMonths = 14,
        SixteenMonths = 16,
        EighteenMonths = 18
    }

    public static class MyExtensions
    {
        public static SelectList ToSelectList(this occupancyTimeline enumObj)
        {
            var values = from occupancyTimeline e in Enum.GetValues(typeof(occupancyTimeline))
                         select new { Id = e, Name = string.Format("{0} Months", Convert.ToInt32(e)) };
            return new SelectList(values, "Id", "Name", enumObj);
        }
    }

}

我要做的是以下内容,取enum occupancyTimeline方法(12,14,16,18)中的值并将其用于下拉菜单,如下所示:

@model CentralParkVIPPreview.Models.CP_VIP_Preview
@using CentralParkVIPPreview.Models

@Html.DropDownListFor(model => model.occupancyTimeline, Model.occupancyTimeline.ToSelectList());

我的问题是我收到此错误:

'string' does not contain a definition for 'ToSelectList' and the best extension method overload 'CentralParkVIPPreview.Models.MyExtensions.ToSelectList(CentralParkVIPPreview.Models.occupancyTimeline)' has some invalid arguments

在这一行:

Model.occupancyTimeline.ToSelectList()

我的问题是,我做错了什么?

2 个答案:

答案 0 :(得分:0)

您的扩展方法适用于枚举类型,并且您尝试将其用于字符串,您可以尝试向模型添加新属性以将字符串解析为枚举,如下所示:

          public occupancyTimeline occupancyTimelineType
            {
                get
                {
                    return Enum.Parse(occupancyTimeline, occupancyTimeline);

                }
            }

在你的cshtml中:

@Html.DropDownListFor(model => model.occupancyTimeline,Model.occupancyTimelineType.ToSelectList());

答案 1 :(得分:0)

[Table("CP-VIP-Preview")]
    public class CP_VIP_Preview
    {
        [Key]
        [DisplayName("Occupancy Timeline")]
        [Required]
        public string occupancyTimeline { get; set; }


        public occupancyTimelineTypes occupancyTimelineType
        {
            get
            {
                return Enum.Parse(occupancyTimelineTypes, occupancyTimeline);

            }
        }
    }

    public class Homefront : DbContext
    {
        public DbSet<CP_VIP_Preview> Data { get; set; }
    }

    public enum occupancyTimelineTypes : int
    {
        TwelveMonths = 12,
        FourteenMonths = 14,
        SixteenMonths = 16,
        EighteenMonths = 18
    }

    public static class MyExtensions
    {
        public static SelectList ToSelectList(this occupancyTimelineTypes enumObj)
        {
            var values = from occupancyTimeline e in Enum.GetValues(typeof(occupancyTimeline))
                         select new { Id = e, Name = string.Format("{0} Months", Convert.ToInt32(e)) };
            return new SelectList(values, "Id", "Name", enumObj);
        }
    }
你的HTML中的

@Html.DropDownListFor(model => model.occupancyTimeline,Model.occupancyTimelineType.ToSelectList());