Data collection between ages in mvc5

2019-08-19 13:00发布

**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条回答
劫难
2楼-- · 2019-08-19 13:42

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

查看更多
登录 后发表回答