创建c# SelectList 不指定值
Create c# SelectList Without specifying values
手动创建 select 列表时,我通常同时拥有文本和值:
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");
有什么想法吗?
抱歉,是的,我希望文本和值相同。
如评论中所建议,您可以使用构造函数重载:
var selectListItems = new[] {"Please Select", "1", "2", "3"};
var selectList = new SelectList(selectListItems, "1");
手动创建 select 列表时,我通常同时拥有文本和值:
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");
有什么想法吗?
抱歉,是的,我希望文本和值相同。
如评论中所建议,您可以使用构造函数重载:
var selectListItems = new[] {"Please Select", "1", "2", "3"};
var selectList = new SelectList(selectListItems, "1");