私のビューでASP.NET MVC 5 Entity Frameworkを使用しています。 toは列挙型を使用してドロップダウンメニューを設定します。これは私が私のクラスで得たものである:Enum.Parse暗黙的に型 'オブジェクト'を変換できません明示的な変換が存在します(キャストがありませんか)
public enum occupancyTimelineTypes : int
{
TwelveMonths = 12,
FourteenMonths = 14,
SixteenMonths = 16,
EighteenMonths = 18
}
と、この:
[DisplayName("Occupancy Timeline")]
[Required]
public string occupancyTimeline { get; set; }
public occupancyTimelineTypes occupancyTimelineType
{
get
{
return Enum.Parse(typeof(occupancyTimelineTypes), occupancyTimeline);
}
}
私の問題は私が解決する方法が分からないエラーを取得しています、次のとおりです。
Cannot implicitly convert type 'object' to An explicit conversion exists (are you missing a cast?)
次のようにドロップダウンメニューに値を設定しています:
、ここでは、場所に置くと、これらのエラーを修正するために、私はHtml.EnumDropDownListFor()
を使用しませんようにあまりにも多くのエラーが表示されることができず、私のToSelectList()
方法
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.DropDownListFor
エラーが示唆していることを試してみましたか? '(キャストがありませんか?)' – RafaelC
エラーメッセージが実際に役に立っていることは数回あります:) – KSib