Enum.Parse不能隐式转换类型“对象”到显式转换存在(你错过了一个演员?) [英] Enum.Parse Cannot implicitly convert type 'object' to An explicit conversion exists (are you missing a cast?)

查看:136
本文介绍了Enum.Parse不能隐式转换类型“对象”到显式转换存在(你错过了一个演员?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC 5实体框架,在我的视图中,我有一个下拉菜单,我正在尝试使用枚举来填充下拉菜单。这是我在课堂上得到的:

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

,这个:

  [DisplayName(Occupancy Timeline)] 
[必需]
public string occupancyTimeline {get;组; }

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

}
}

我的问题是,我正在一个错误我不知道如何修复:

 不能隐式转换类型对象到显式转换存在(你错过了一个演员?)

我正在填充我的下拉菜单:

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

这里是我的 ToSelectList()方法public static class MyExtensions
{
public static SelectList ToSelectList(this occupancyTimelineTypes enumObj)
{
var values =从Enum.GetValues(typeof(occupancyTimeline))中的occupancyTimeline e
选择新的{Id = e,Name = string.Format({0} Months,Convert.ToInt32(e ))};
返回新的SelectList(值,Id,Name,enumObj);
}
}

我不能使用,不会使用 Html.EnumDropDownListFor(),因为出现太多错误,并且它是一个噩梦来实现并修复这些错误。



这必须是 @ Html.DropDownListFor

解决方案

枚举。解析返回对象(它不是通用的),所以你需要显式地转换你的返回值。使用:

  return(occupancyTimelineTypes)Enum.Parse(typeof(occupancyTimelineTypes),occupancyTimeline); 


I am using ASP.NET MVC 5 Entity Framework, in My view I have a dropdown menu, what I am trying to is use enum to populate dropdown menus. This is what I got in my class:

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

and this:

[DisplayName("Occupancy Timeline")]
        [Required]
        public string occupancyTimeline { get; set; }

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

            }
        }

my problem is, I am getting an error I have no idea how to fix:

Cannot implicitly convert type 'object' to An explicit conversion exists (are you missing a cast?)

I am populating my dropdown menu like so:

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

and here is my ToSelectList() Method

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);
        }
    }

I CANNOT AND WILL NOT USE Html.EnumDropDownListFor() as too many errors appear and its a nightmare to put in place and fix these errors.

This has to be @Html.DropDownListFor

解决方案

Enum.Parse returns object (it is not generic) so you need to explicitly cast your return value. Use:

return (occupancyTimelineTypes)Enum.Parse(typeof(occupancyTimelineTypes), occupancyTimeline);

这篇关于Enum.Parse不能隐式转换类型“对象”到显式转换存在(你错过了一个演员?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆