Data collection between ages in mvc5

喜夏-厌秋 提交于 2019-12-12 01:27:26

问题


**hi gays I have tried to collect data between ages but there is an error I do not know why here is my code my controller **

 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();
    }

and this is my model for To collect value

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

and this is my view

<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>

i Can't find the problem can you help me

the problem in my controller here enter image description here


回答1:


jmal hassn

public IEnumerable Count { get; set; }

But you are passing Count which is an int by itself

select new UserRange { AgeRange = g.Key, Count = g.Count() }

You have to pass a list of pations and than you count on that



来源:https://stackoverflow.com/questions/53652726/data-collection-between-ages-in-mvc5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!