you need to do like this:
ViewBag.CountryIdList = new SelectList(db.Countries.Where(a => a.Currency != null), "Id", "Name",1);
For Example in the Countries List, you have a item CountryName and its id is 1, you need to pass the 1 in the last parameter, and element with that Id 1 will be shown selected by default.
Example:
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
List<Country> list = new List<Country>();
list.Add(new Country{ Id = 1, Name="Test"});
list.Add(new Country{ Id = 2, Name="Test2"});
now in controller action:
int Selected = 2;
ViewBag.CountryIdList = new SelectList(list, "Id", "Name",Selected);
Now the Test2 will be shown selected default in View.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…