Once setting the .DataSource of a dropdownlist to a view model. How can I immediately tell the dropdownlist to select a specific value within the model?
// UI
ReferenceProjectDropDownList.DisplayMember = "Name";
ReferenceProjectDropDownList.DataSource = new ProjectsViewModel(snc.Measuring.Projects.ToList()).Projects;
if (null != project)
{
ReferenceProjectDropDownList.SelectedItem = ???
}
// view model
public class ProjectsViewModel
{
public ProjectsViewModel(List<ScanProject> projects)
{
Projects = new List<ProjectViewModel>();
projects.ForEach(p =>
{
Projects.Add(new ProjectViewModel()
{
Id = p.Id,
Name = p.Name,
});
});
}
public List<ProjectViewModel> Projects;
}
public class ProjectViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}