我正在使用 Razor 模板和实体构建基于 ASN.NET MVC3 的网页。任务是从数据库中填充一个 @Html.DropDownList,到目前为止还不错,但现在我想在 DropDownList 中插入一个静态值。
代码如下
创建方法
public ActionResult Create()
{
var parents = db.Organizations.OrderBy(o => o.Title).ToList();
var organization = new Organization();
return View(organization);
}
从 Razor 模板中提取
<div class="editor-field">
@Html.DropDownList("ParentGuid",
new SelectList(ViewBag.Organizations as System.Collections.IEnumerable,
"ParentGuid", "Title", Model.ParentGuid))
</div>
所以,上面的代码可以工作,但我一直在弄清楚如何在下拉列表中插入一个空行。
请您参考如下方法:
我不太熟悉 @ 但是,如果您尝试过重载?
@Html.DropDownList("ParentGuid",
new SelectList(ViewBag.Organizations as System.Collections.IEnumerable,
"ParentGuid", "Title", Model.ParentGuid), "Your empty option text here")
