Search code examples
asp.net-mvc-4razormodel-view-controllerasp.net-mvc-5

Data collection between ages in mvc5


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

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

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>

The problem in my controller here enter image description here


Solution

  • 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