Reputation: 2139
This has been answered multiple times, but I have tried to follow the answers and still no success.
Error message:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'MovieID'
My model:
public class Show
{
public int ID { get; set; }
public int MovieID { get; set; }
public int TheaterID { get; set; }
public DateTime StartTime { get; set; }
public string BookingLink { get; set; }
public string StartTimeAsString { get; set; }
public Movie Movie { get; set; }
public Theater Theater { get; set; }
}
Where it crashes:
<div class="col-md-10">
@Html.DropDownList("MovieID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.MovieID, "", new { @class = "text-danger" })
</div>
<div class="col-md-10">
@Html.DropDownList("TheaterID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TheaterID, "", new { @class = "text-danger" })
</div>
The view is autogenerated through the "Create" template. Any idea of how to fix this?
Upvotes: 0
Views: 39
Reputation: 23690
You are not assigning an IEnumerable<SelectListItem>
anywhere within your DropDownFor
helpers as it expects.
Your Show
class should contain a property for both select lists, so that the drop down knows what items to display:
public class Show
{
public int ID { get; set; }
public int MovieID { get; set; }
public int TheaterID { get; set; }
public DateTime StartTime { get; set; }
public string BookingLink { get; set; }
public string StartTimeAsString { get; set; }
public Movie Movie { get; set; }
public Theater Theater { get; set; }
public IEnumerable<SelectListItem> Theatres { get; set; }
public IEnumerable<SelectListItem> Moview { get; set; }
}
You should populate these properties in your Model before you pass them to your view and then pass them into your Html.DropDownListFor
helpers:
<div class="col-md-10">
@Html.DropDownListFor(x => x.MovieID, model.Movies, null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.MovieID, "", new { @class = "text-danger" })
</div>
<div class="col-md-10">
@Html.DropDownListFor(x => x.TheaterID, model.Theatres, null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TheaterID, "", new { @class = "text-danger" })
</div>
Upvotes: 1