ASP MVC DropDownList有额外的值(整数id)

时间:2017-04-21 16:05:21

标签: asp.net-mvc

我最终在我的DropDown中有三个值...两个预期值和一个附加值,它是selecteditem的主键,即。 “2” 如何摆脱“2”,仍然选择合适的项目?

enter image description here

        @Html.DropDownListFor(x => x.ComicBookId, ComicNet.Models.ComicBook.GetValues(), Model.ComicBookId.ToString(), new { @id = "ComicBookId" })


 public partial class ComicBook
    {
        public static SelectList GetValues()
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {

                SelectList selectList = new SelectList(db.ComicBooks.ToList(), "ComicBookId", "Name");

                return selectList; 

            }
        }
}

1 个答案:

答案 0 :(得分:2)

只需删除第三个参数,因为它设置了optionLabel

@Html.DropDownListFor(x => x.ComicBookId, ComicNet.Models.ComicBook.GetValues(), new { @id = "ComicBookId" })

这是您正在使用的重载:SelectExtensions.DropDownListFor Method (HtmlHelper, Expression>, IEnumerable, String, Object)

这是您想要的人:SelectExtensions.DropDownListFor Method (HtmlHelper, Expression>, IEnumerable, Object)

相关问题