C# 手动将 SelectListItem 添加到 SelectList 以在 DropDownListFor 中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29869508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding SelectListItem manually to SelectList to use in DropDownListFor
提问by Marnus Steyn
When I create a SelecList I wish to be able to add SelecListItem's manually and to do this I use this code:
当我创建一个 SelecList 时,我希望能够手动添加 SelecListItem,为此我使用以下代码:
List<SelectListItem> Provinces = new List<SelectListItem>();
Provinces.Add(new SelectListItem() { Text = "Northern Cape", Value = "NC" });
Provinces.Add(new SelectListItem() { Text = "Free State", Value = "FS" });
Provinces.Add(new SelectListItem() { Text = "Western Cape", Value = "WC" });
SelectList lstProvinces = new SelectList(Provinces);
Instead of this :
而不是这个:
var lstProvinces = new SelectList(new[] { "Northern Cape", "Free State", "Western Cape" });
After I created the SelectList, I pass it to the DropDownListFor via the ViewBag :
创建 SelectList 后,我通过 ViewBag 将其传递给 DropDownListFor :
Html.DropDownListFor(m => m.StartPointProvince, (SelectList)ViewBag.Provinces)
However when I create the SelectList using the first method, it doesn't work - It adds the 3 values to the dropdown list, but all the values display as:
*screenshot of output
但是,当我使用第一种方法创建 SelectList 时,它不起作用 - 它将 3 个值添加到下拉列表中,但所有值都显示为:
*输出屏幕截图
However when I use the second method, it works fine. I wish to use the first method because i want to be able to specify the Text AND value of each item.
但是,当我使用第二种方法时,它工作正常。我希望使用第一种方法,因为我希望能够指定每个项目的 Text AND 值。
采纳答案by Eugene Podskal
The problem is that SelectList(IEnumerable)constructor doesn't accept SelectListItem's(at least not as SelectListItemto add to its Itemscollection). It simply accepts collection of some arbitrary objects that will be used to generate completely unrelated internal SelectListItems collection.
问题是SelectList(IEnumerable)构造函数不接受SelectListItem's(至少不会SelectListItem添加到其Items集合中)。它只是接受一些任意对象的集合,这些对象将用于生成完全不相关的内部SelectListItem集合。
If you want, you can use SelectList(IEnumerable, string, string)constructor in such way:
如果你愿意,你可以这样使用SelectList(IEnumerable, string, string)构造函数:
List<SelectListItem> Provinces = new List<SelectListItem>();
Provinces.Add(new SelectListItem() { Text = "Northern Cape", Value = "NC" });
Provinces.Add(new SelectListItem() { Text = "Free State", Value = "FS" });
Provinces.Add(new SelectListItem() { Text = "Western Cape", Value = "WC" });
this.ViewBag.Provinces = new SelectList(Provinces, "Value", "Text");
It will work. But it is unnecessary, because you create complex SelectListItemitems that won't be used by the SelectList- it will just treat them as any other data object.
它会起作用。但这是不必要的,因为您创建了SelectListItem不会被 使用的复杂项目SelectList- 它只会将它们视为任何其他数据对象。
In the same way you can just use some other simpler class in place of SelectListItem:
以同样的方式,您可以使用其他一些更简单的类来代替SelectListItem:
public class SelectListModel
{
public String Text { get; set; }
public String Value { get; set; }
}
...
Provinces.Add(new SelectListModel() { Text = "Northern Cape", Value = "NC" });
回答by Joe
Use DropDownList and name it the same as the model's property name. Mine is "ItemType"
使用 DropDownList 并将其命名为与模型的属性名称相同的名称。我的是“项目类型”
@Html.LabelFor(model => model.ItemType, new { @class = "control-label" })
@Html.DropDownList("ItemType", (IEnumerable<SelectListItem>)ViewBag.ItemTypes, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ItemType, null, new { @class = "text-danger" })
var types = new List<SelectListItem>();
types.Add(new SelectListItem() { Text = "Select...", Value = string.Empty });
types.Add(new SelectListItem() { Text = "OTC", Value = "0" });
types.Add(new SelectListItem() { Text = "Generic", Value = "1" });
types.Add(new SelectListItem() { Text = "Brand", Value = "2" });
types.Add(new SelectListItem() { Text = "Non-Merchandise", Value = "9" });
ViewBag.ItemTypes = types;
[Required(ErrorMessage = "Item Type is required")]
public Int32 ItemType { get; set; }
回答by Tom Wass
you can change your code from
你可以改变你的代码
SelectList lstProvinces = new SelectList(Provinces);
to
到
SelectList lstProvinces = new SelectList(Provinces, "Value", "Text");
and it will display provinces correctly.
它会正确显示省份。

