简体   繁体   English

ViewBag 选择列表返回年份,使用 GroupBy 消除同一年的多个年份

[英]ViewBag selectlist to return Year using GroupBy to eliminate multiple years of the same year

I am having some issues creating a viewbag selectlist.我在创建 viewbag 选择列表时遇到了一些问题。 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.目标是从 select 创建一个年份的下拉列表。 I think my controller is good but not sure and the view is definitely not correct.我认为我的 controller 不错,但不确定,观点肯定不正确。

Here is what I have:这是我所拥有的:

Controller: 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. controller 在调试时计数为 2,这是正确的,只有 2021 和 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.我不确定为什么它说没有CreatedDate

Thanks for your help!谢谢你的帮助!

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" })

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.以下是我在 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 .这对我来说似乎是多余的,但它可以工作,并且不能与var list一起使用.ToList Meaning on the Viewbag.Year when putting Value = x. and Text = x.放置Value = x. and Text = x.Viewbag.Year上的含义。 Value = x. and Text = x. It did not know what Text & Value was.它不知道Text & Value是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM