【问题标题】:cant figure out whats wrong with my enum dropdown list in mvc无法弄清楚我在 mvc 中的枚举下拉列表有什么问题
【发布时间】:2015-01-15 01:06:25
【问题描述】:

我将索引页面上的列表中的数据播种到表格中,这是专辑使用实体框架创建它的模型,这就是全部 Model.cs

public enum genre { Rock=1, Pop=2 }

public class Album
{
    public int AlbumID { get; set; }
    public string AlbumName { get; set; }
    public string AlbumArtist { get; set; }
    public genre AlbumGenre { get; set; }
    public Album()
    {
        GenreList = new List<SelectListItem>();
    }
    public IEnumerable<SelectListItem> GenreList { get; set; } 
    public List<Artist> Artists { get; set; }  
}

控制器的下拉列表代码如下。我在最后的代码中的model 下有一条红线,这里是return View(model); 但是当我运行它时,它并没有将其指定为答案。

public ActionResult Index()
{
    Album model = new Album();
    IEnumerable<genre> genres = Enum.GetValues(typeof (genre)).Cast<genre>();
    model.GenreList = from action in genres
                      select new SelectListItem
                      {
                          Text = action.ToString(),
                          Value = (action.ToString())
                      };
    return View(model);
}

我的索引页是显示错误的地方。 @Html.DropDownListFor(model =&gt; model.AlbumID, model.GenreList) 是我在 AlbumIdGenreList 上收到错误的行

@model IEnumerable<revision.Models.Album>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create WOW", "Create"
</p>
@Html.DropDownListFor(model => model.AlbumID, Model.GenreList)

【问题讨论】:

  • 你不应该绑定到属性AlbumGenre(不是AlbumID)吗?

标签: asp.net-mvc-4 model enums html.dropdownlistfor selectlistitem


【解决方案1】:

在 MVC 5.1 的 Razor 视图中支持枚举。检查您是否使用任何先前版本的 mvc。此外,MVC 5.1 Razor DisplayFor not working with Enum DisplayName 这也会对您有所帮助。

【讨论】:

  • OP 已标记问题 MVC-4!
猜你喜欢
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
相关资源
最近更新 更多