user450549
user450549

Reputation:

Setting up a dropdownlist with a ViewModel with IEnumerable custom class

I have changed my ddl to use the view model instead of viewbag, but I am getting this error:

There is no ViewData item of type 'IEnumerable'

In my ViewModel:

public IEnumerable<TimeZone> TimeZones
{
   List<TimeZone> timeZones = new List<TimeZone>();
   timeZones.Add(new TimeZone { TimeZoneName = "Eastern", TimeZoneOffset = -5 });
}

My View:

@Html.DropDownList("timeZone", Model.TimeZones as SelectList, new { @class = "ddl" })

my automapping

.ForMember(dest => dest.TimeZones, opt => opt.Ignore());

Upvotes: 0

Views: 380

Answers (1)

Knaģis
Knaģis

Reputation: 21485

Model.Timezones is of type List<TimeZone> in your example.

List<TimeZone> cannot be converted to SelectList so the code Model.TimeZones as SelectList will return null.

Instead of the cast, use new SelectList(Modle.TimeZones, "TimeZoneOffset", "TimeZoneName").

Upvotes: 0

Related Questions