Home > Software engineering >  ViewBag selectlist to return Year using GroupBy to eliminate multiple years of the same year
ViewBag selectlist to return Year using GroupBy to eliminate multiple years of the same year

Time:02-06

I am having some issues creating a viewbag selectlist. I am grabbing from a table where there are multiple records with the same date - Year. The Goal is to create a dropdown of years to select from. I think my controller is good but not sure and the view is definitely not correct.

Here is what I have:

Controller:

    public ActionResult MilesReport(long? Year)
    {
        long Years = Year ?? db.ExpenseReportsDetails.FirstOrDefault().CreatedDate.Year;
        var list = db.ExpenseReportsDetails.GroupBy(i => i.CreatedDate.Year).ToList();
        ViewBag.Year = new SelectList(list, "CreatedDate.Year", "CreatedDate.Year", Years);

        //var miles = db.ExpenseReportsDetails.Where(x => x.Miles > 0 && x.CreatedDate.Year == Year);

        return View();
    }

And here is the View:

@Html.DropDownList("Year", "--Select--")

The controller while in debug gets a count of 2 which is correct there is only 2021 and 2022.

I get this error on the view Dropdown:

DataBinding: 'System.Data.Entity.Core.Objects.ELinq.InitializerMetadata Grouping`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[BestenEquipment.GeneralDTO.Entities.ExpenseReportsDetails, BestenEquipment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not contain a property with the name 'CreatedDate'.

I am not sure why it says there is not a CreatedDate when there is.

Thanks for your help!

CodePudding user response:

try this

 var list = db.ExpenseReportsDetails.Select(s=> s.CreatedDate.Year).Distinct(). ToList();
 ViewBag.Year = list.Select(i=> new SelectListItem{ Value=s.ToString(), Text = s.ToString()}.ToList();

view

 @Html.DropDownListFor(model => model.Year, @ViewBag.Year, "select", new { @class = "form-control" })

CodePudding user response:

I did not have to use this on the above. However I did use it for another page. Below is what I found to work with the help of the answer by Serge. It did not work the way he had it but I was able to reconstruct it so it did. Here is what the solution ended up being.

 AccountingEntities acct = new AccountingEntities();
 var list = acct.Transaction.Select(s => s.TransactionDate.Year).Distinct().Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString() });
 ViewBag.Year = list.Select(x => new SelectListItem
                                 {
                                    Value = x.Text,
                                    Text = x.Text
                                 }).ToList();

Then in the View I added this:

 @Html.DropDownList("Year", " -Select- ")

This does seem redundant to me but it works, and did not work with the var list to just a .ToList. Meaning on the Viewbag.Year when putting Value = x. and Text = x. It did not know what Text & Value was.

  •  Tags:  
  • Related