【发布时间】: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 => model.AlbumID, model.GenreList) 是我在 AlbumId 和 GenreList 上收到错误的行
@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