asreeram
asreeram

Reputation: 31

how to add a value to SelectList in C#

I want to know how to attach a SelectListItem to a SelectList object :

I create an SelectListItem as :

SelectListItem sl = new SelectListItem() { Text = "-99", Value = "Select a Value" };

Now I want to attach the item to below SelectList:

public SelectList lstCCPerfOrg { get; set; }

Please help.

Thanks,

Upvotes: 1

Views: 2853

Answers (2)

Mike
Mike

Reputation: 721

var selectList = new SelectList(
        new List<SelectListItem>
        {
            new SelectListItem {Text = "-99", Value = "Select a Value"}
        }, "Value", "Text");

Upvotes: 1

SelectList Represents a list that lets users select one item.Hence the input should be list of items. The SelectList has accept only IEnumarable of Items.

        List<SelectListItem> sl = new List<SelectListItem>()
        {
            new SelectListItem() { Text = "-99", Value = "Select a Value" }
        };

        SelectList lstCCPerfOrg = new SelectList(sl);

Upvotes: 1

Related Questions