0

Here is my code gives me error while add countrylist to listofcountries

ERROR

system.collection.generic.list is not assignable to parameter type system.web.mvc.selectListitem

CODE

public List<SelectListItem> Getallcountries()
{
    var listofcountries = new List<SelectListItem>();
    using (var db = new DatabaseContext())
    {
        var countrylist = db.Countries.Where(x => x.IsActive == true).ToList().Select(x => new SelectListItem
        {
            Text = x.CountryName,
            Value = Convert.ToString(x.ID)
        });

       listofcountries.Add(countrylist);
    }
    return listofcountries;
}
L-Four
  • 13,345
  • 9
  • 65
  • 109

2 Answers2

1

Return the list generated by code directly. No need to add it to another list

public List<SelectListItem> Getallcountries()
{

    using (var db = new DatabaseContext())
    {
        var countrylist = db.Countries.Where(x => x.IsActive == true).ToList().Select(x => new SelectListItem
        {
            Text = x.CountryName,
            Value = Convert.ToString(x.ID)
        });

       return  countrylist.ToList();
    }

}
Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

Try This

 using(var db=new dbContect()
  {
      var count=db.Countries.Where(x=>x.IsActive==true).Select(a=>new SelectListItem{
    Text=a.CountyName,
    Value=SqlFunctions.StringConvert(a.ID)
}).ToList();

return count; }

Own
  • 174
  • 1
  • 11