首页 > 解决方案 > mvc5中年龄之间的数据收集

问题描述

我试图收集年龄之间的数据,但有一个错误我不知道为什么这是我的代码我的控制器

 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>

我的控制器中的问题在此处输入图像描述

标签: asp.net-mvc-4razormodel-view-controllerasp.net-mvc-5

解决方案


杰马尔·哈森

公共 IEnumerable 计数 { 获取;放; }

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

选择新用户范围 { AgeRange = g.Key, Count = g.Count() }

你必须通过一个列表,而不是指望那个


推荐阅读