创建c#SelectList而不指定值

时间:2019-04-18 07:54:26

标签: c# .net-core selectlist

手动创建选择列表时,我通常同时具有文本和值:

SelectList SupplierStatusList = new SelectList(new[] {
            new SelectListItem { Selected = false, Text = "Please Select", Value = "Please Select" },
            new SelectListItem { Selected = true, Text = "1", Value = "1" },
            new SelectListItem { Selected = false, Text = "2", Value = "2" },
            new SelectListItem { Selected = false, Text = "3", Value = "3" },
            new SelectListItem { Selected = false, Text = "4", Value = "4" },
            }, "Value", "Text");

但是如何在不必指定值的情况下执行此操作?这样就留下了我不想要的值=“”。

SelectList SupplierStatusList = new SelectList(new[] {
            new SelectListItem { Selected = false, Text = "Please Select"},
            new SelectListItem { Selected = true, Text = "1"},
            new SelectListItem { Selected = false, Text = "2"},
            new SelectListItem { Selected = false, Text = "3"},
            new SelectListItem { Selected = false, Text = "4"},
            }, "Value", "Text");

有什么想法吗?

对不起,很清楚,是的,我希望文本和值相同。

1 个答案:

答案 0 :(得分:2)

如注释中所建议,您可以使用构造函数重载:

var selectListItems = new[] {"Please Select", "1", "2", "3"};
var selectList = new SelectList(selectListItems, "1");