net MVC, i searched on too many sites to fetch data from database into Listbox but i didn't get any proper answer,
I every site there is a hardcoded example for List<> like this,
public SelectList GetAllCountryList()
{
List<Country> objcountry = new List<Country>();
objcountry.Add(new Country { Id = 1, CountryName = "India" });
objcountry.Add(new Country { Id = 2, CountryName = "USA" });
objcountry.Add(new Country { Id = 3, CountryName = "Pakistan" });
objcountry.Add(new Country { Id = 4, CountryName = "Nepal" });
SelectList objselectlist = new SelectList(objcountry, "Id", "CountryName");
return objselectlist;
}
this id the hard coded but i want to fetch it from db and i know i have to apply query like this
public List<Country> allname = new List<Country>
{
//query
};
but i dont know how should i use it,or whether this is correct or not
please help to solve this