Reputation: 756
When manually creating a select list I normally have both text & value:
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");
But how do I do this without having to specify the value? This leaves the values = "" which I don't want.
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");
Any ideas?
Sorry, to be clear yes I would like the text and value to be the same.
Upvotes: 2
Views: 101
Reputation: 1689
As suggested in the comments, you can use a constructor overload:
var selectListItems = new[] {"Please Select", "1", "2", "3"};
var selectList = new SelectList(selectListItems, "1");
Upvotes: 3