【问题标题】:DropDownListFor not selecting properly from enumDropDownListFor 没有从枚举中正确选择
【发布时间】:2014-05-21 18:43:24
【问题描述】:

我是 MVC 新手,在从枚举加载的 DropDownListFor 中选择所需值时遇到了麻烦。

这是我的枚举:

public enum MobilePlatform
{
    iOS = 0,
    Android = 1,
    WindowsPhone = 2,
}

这是我的 DropDownListFor

@Html.DropDownListFor(model => model.ActiveGame.mobile_platform, Enum.GetNames(typeof(DeveloperPortalMVCApp.Models.MobilePlatform)).Select(e => new SelectListItem { Text = e }))

model.ActiveGame.mobile_platform 带有一个 int 2,但是当我加载该视图时,我总是得到 iOS。我做错了什么?

【问题讨论】:

    标签: asp.net-mvc enums html.dropdownlistfor


    【解决方案1】:

    添加这个

    e => new SelectListItem { Text = e, Selected = (e == MobilePlatform.iOS.ToString()) })
    

    【讨论】:

    • 考虑添加更多关于此代码为何有效的评论/理由。简单地输入代码会促进复制和粘贴,而不会清楚地了解发生了什么。
    • 这不是总是返回 MobilePlatform.iOS 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多