0

In a SelectList object, I want that the Text property of each element of the SelectList should also be the value. For eg:

SelectList s=new SelectList(some list);
s.ElementAt(i).Value=s.ElementAt(i).Text;

Is there any way to do this APART from using a loop? Is there any inbuilt method or something like that to accomplish this?

karan k
  • 947
  • 2
  • 21
  • 45

2 Answers2

1

You can use the following Constructor overload for SelectList:

public SelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField
)

You can then, provided same list member for dataValueField and dataTextField.

Ebad Masood
  • 2,389
  • 28
  • 46
1

If you really want to do it without a loop, just use linq foreach (which is a loop in the background):

s.ToList().ForEach(c=>c.Text = c.Value)
Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93