Search code examples
c#listmodel-view-controllerinsertdropdownlistfor

How to insert an initial values for the list, which have already added data in it, mvc, c#


I have added data to a list like this.

List<CommonSearch> regionList = new List<CommonSearch>();

            foreach(var items in filteredRegion)
            {
                regionList.Add(new CommonSearch 
                {
                    Destination = returnRegionName(items),
                    regCde = items
                });

            }

now I want to add initial data for this.like below to show in my dropdownlist.how can I do that.

//regionList.Insert(0,"All");


Solution

  • Insert into regionList new CommonSearch item after foreach loop:

    regionList.Insert(0, new CommonSearch() { Destination = "All" });
    

    Your insert code won't work because of type mismatch in your list.