【发布时间】:2019-06-05 10:12:15
【问题描述】:
我已经填充了一个 viewbag 并将一个下拉列表绑定到它..如下所示
var countrylist = ctx.Countries.ToList();
ViewBag.countrylist = new SelectList(countrylist, "ID", "Name",2);
@Html.DropDownList("countrylist", (IEnumerable<SelectListItem>)ViewBag.countrylist, "-Select-")
上面我想默认选择 countrylist 的第二项,所以我提到了值“2”作为 ViewBag countrylist 重载的最后一个参数。 但默认情况下它不会选择该项目。
但是,如果我将下拉列表名称更改为如下所示的其他名称
@Html.DropDownList("countrylist12345", (IEnumerable<SelectListItem>)ViewBag.countrylist, "-Select-")
那么它会默认选择国家列表中的第二项。
请解释为什么会这样?
【问题讨论】: