Search code examples
c#c#-4.0selectlistitem

Modify the SelectListItem getter to return a modified Text Value


I have a class that has a IEnumerable<SelectListItem> as one of its properties. I'm using this list to populate a @Html.DropDownListFor() Drop down list - this is working and I am getting the values I expect in my dropdown.

However I want to modify the text value that shows in the drop down list.

I think one way to do this would be to modify the value in the Getter something like this:

    public IEnumerable<SelectListItem> ShippingQuotes
    {
        get { return ShippingQuotes; }//SomethingHere to modify the text property of the Select List Items; 

        set { ShippingQuotes = value; }
    }

Can someone help me with the syntax here?


Solution

  • You can simply modify its text using Select():

    public IEnumerable<SelectListItem> ShippingQuotes
    {
         get { return shippingQuotes.Select(x => {x.Text = "Some Text" + x.Text; return x;}); } 
         set { ShippingQuotes = value; }
    }