【问题标题】:Data collection between ages in mvc5mvc5中年龄之间的数据收集
【发布时间】:2019-05-08 05:28:30
【问题描述】:

我尝试收集不同年龄段的数据,但出现错误我不知道为什么这是我的代码我的控制器

 public ActionResult AllCuont()
    {
        var query = (from t in db.Pations
                     let range = (
                                  t.Age >= 0 && t.Age < 10 ? "0-9" :
                                  t.Age >= 11 && t.Age < 15 ? "10-14" :
                                  t.Age >= 15 && t.Age < 50 ? "15-50" :
                                  "50+"
                                  )
                     group t by range into g
                     select new UserRange { AgeRange = g.Key, Count = g.Count() }).ToList();
        //add the sum column.
        query.Add(new UserRange() { AgeRange = "Sum", Count = query.Sum(c => c.Count) });

        ViewBag.UserData = query;
        return View();
    }

这是我收集价值的模式

  namespace Archive.Models
{
    public class UserRange
    {
        public string AgeRange { get; set; }
        public IEnumerable<Pation> Count { get; set; }
    }
}

这是我的看法

<table border="1">

<tr>
    @foreach (var item in ViewBag.UserData)
    {
        <th>@item.AgeRange </th>
    }

</tr>
<tr>
    @foreach (var item in ViewBag.UserData)
    {
        <td>@item.Count </td>
    }
</tr>

我的控制器中的问题enter image description here

【问题讨论】:

    标签: asp.net-mvc-4 razor model-view-controller asp.net-mvc-5


    【解决方案1】:

    jmal hassn

    公共 IEnumerable 计数 { get;放; }

    但是你传递的 Count 本身就是一个 int

    选择新的 UserRange { AgeRange = g.Key, Count = g.Count() }

    你必须传递一个 pations 列表,然后你指望它

    【讨论】:

    • 是的,兄弟,谢谢你,我的朋友。有效。非常感谢你,兄弟。我很感谢你和我站在一起,兄弟,我真的很感谢你,mamoon
    • @jmalhassn 亲爱的,不客气,stack-overflow 就像我们的家人一样是我们的社区.. 快乐的编码兄弟
    • 嗨 Mamoon Rasheed 你好吗我有一个小问题,如果我不显示这些数据,但按月我测量这个年龄但按月分组
    • @jmalhassn 我做得很好,谢谢你的提问,因为你多年来一直这样做,现在转换几个月不会有问题,上面这些你是你给的几年的范围,所以它相当灵活到任何给定的数字,只需根据您的观点进行转换。希望这会有所帮助,如果不是像以前一样发布问题或图片会找到一种适合您考虑的方法
    • 嗨 Mamoon Rasheed 看看这个问题,但我添加了一些东西
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 2019-05-04
    • 2011-08-11
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多