@ Html.DropDownListFor如何添加选项?
@Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles))
上面的代码创建一个选择列表就好了。但我想让选择可选。不幸的是,没有空的选择,我想添加一个。我该怎么做?@ Html.DropDownListFor如何添加选项?
通过使用proper DropDownListFor overload:
@Html.DropDownListFor(
model => model.ZipFile,
new SelectList(ViewBag.ZipFiles),
"-- please select a zip file --"
)
Ahh ok完美,谢谢! – 2012-02-15 13:30:26
查看我的DDL教程http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc和http ://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT 2012-02-15 19:20:43
太棒了,谢谢! – Lee 2016-11-09 06:21:25
在控制器中,当您设置ViewBag.ZipFiles时,将SelectListItem添加到该集合。
有一个更简单的方法。 – 2012-02-15 13:14:40
是的,我想这也可以。甚至没有想到它。 – 2012-02-15 14:13:11
不错 - 我喜欢你的方法。 – 2012-02-15 15:40:04
@Html.DropDownListFor(model => model.Country, new List<SelectListItem>
{
new SelectListItem { Text = "India", Value = "1"},
new SelectListItem { Text = "USA", Value = "2"},
new SelectListItem { Text = "Sreelanka", Value = "3"},
new SelectListItem {Text = "Africa",Value="4"},
new SelectListItem { Text = "China", Value = "5" },
new SelectListItem { Text = "Austraila", Value = "6" },
new SelectListItem { Text = "UK", Value = "7" }
}, "Select Country",
new {@Style = "Width:500px;height:40px;",
@class = "form-control input-lg"})
见我DDL教程http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and- jquery/using-the-dropdownlist-helper-with-aspnet-mvc和http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc。 aspx – RickAndMSFT 2012-02-15 19:20:36