2

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.

3
  • Hello, I mean the newer .NET 5. Commented Sep 10, 2021 at 8:30
  • you also forgot to set your model's property: bdgfixmonths.Years = GetAllYears() Commented Sep 10, 2021 at 8:30
  • Yes sorry, I inlcuded the variable declaration now. Commented Sep 10, 2021 at 8:35

1 Answer 1

1

Since you're returning a model that contains a list of Bdgfixmonths, it's probably better to create a view model that looks like this:

public class BdgfixmonthsViewModel
{
   public List<Bdgfixmonth> Bdgfixmonths { get; set; }
   public IEnumerable<SelectListItem> Years { get; set; }
}

Then, your Action Method will change to:

private readonly salesContext _context;

public bdgfixmonthController(salesContext context)
{
    _context = context;
}

public async Task<IActionResult> Index()
{
    var vm = new BdgfixmonthsViewModel();
    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;
    }

    vm.Years = GetAllYears();
    vm.Bdgfixmonths = bdgfixmonths; 

    return View(vm);
}

Your ViewModel will have the two collections which can then be used in the view. You will need to update the View to reference the Bdgfixmonth properties like Bdgfixmonths.Bbudget for example. Also, the model will change to something like:

@model IEnumerable<Bdgfixmonth>

becomes

@model BdgfixmonthsViewModel

And then any iteration code will change to:

@foreach (var item in Model.Bdgfixmonths)
    {
      item.Bbudget
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Years = GetAllYears(); does not work, the error message is " 'List<Bdgfixmonth>' does not contain a definition for 'Years' and no accessible extension method 'Years' accepting a first argument of type 'List<Bdgfixmonth>' could be found (are you missing a using directive or an assembly reference?). "
@chdev - my bad, just noticed you're returning a collection of Bdgfixmonths, I will alter my answer now.
Yes I did but as I am new I need a minimal reputation to have the upvotes I make show up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.