jimmy
jimmy

Reputation: 756

Create c# SelectList Without specifying values

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

Answers (1)

Lennart Stoop
Lennart Stoop

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

Related Questions