In my MVC application, I need to add a dropdown that would show a list of domain names.
I already have a ViewModel that contains multiple properties. I am not sure what the sequence of the steps should be:
- Add a new property to my
ViewModel
? What should be the type? List? - Define a method that populates the above property with values.
- Use that property in the View? Use
HTML.DropdownFor
?
I know I should put some code in my Question, but right now I am having difficulty getting started with this...
EDIT: Added the following property to the ViewModel:
public IEnumerable<SelectListItem> DomainList { get; set; }
and implemented a method to return a list of Domains:
internal static List<Domain> FetchAllDomains()
Next in my controller action, I have:
var domains = FetchAllDomains().Select(d => d.DomainName);
return new EmailModel() {DomainList = domains };
But I get the following error:
See Question&Answers more detail:osCannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)