In my app I'm trying to populate a drop-down menu by taking data from the db with EF Core, but unfortunately I've been trying it for two days without success.
I have tried to do this as recommended in this post, but I keep getting errors.
More specifically the error I'm facing now is
'IEnumerable' does not contain a definition for 'Years' and no accessible extension method 'Years' accepting a first argument of type 'IEnumerable' could be found
This error comes from the view:
@Html.DropDownList(m => m.Years, Model.Years, "-- Select year --")
Here is my model class:
public partial class Bdgfixmonth
{
public int Counter { get; set; }
public int Byear { get; set; }
//
public IEnumerable<SelectListItem> Years { get; set; }
public string Bbudget { get; set; }
public int Bmonth { get; set; }
public string Blongmonth { get; set; }
public int Closed { get; set; }
public string Current { get; set; }
}
And here is my controller for the GET action method Index:
private readonly salesContext _context;
public IEnumerable<SelectListItem> Years { get; set; }
public bdgfixmonthController(salesContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var bdgfixmonths = await _context.Bdgfixmonths.ToListAsync();
IEnumerable<SelectListItem> GetAllYears()
{
IEnumerable<SelectListItem> list = _context.Bdgfixmonths
.AsEnumerable()
.Select(s => new SelectListItem
{
Selected = false,
Text = s.Byear.ToString(),
Value = s.Byear.ToString()
});
return list;
}
Years = GetAllYears();
return View(bdgfixmonths);
}
The goal here is to populate the drop-down menu with the data of the db, and then make a query based on the selected value, so that only the relevant values are shown (in this case based on the year).
Any help would be very appreciated, thanks.