Cant iterate through model values in view mvc.net

Viewed 48

Im wokring on a project that is intended to function as a parking application. Now im trying to create a report that shows how many cars have parked for specific hours in specific days. To achieve this with help i have a model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Parkim_Task.Models
{
    public class raporte
    {
        public DateTime hyrje { get; set; }
        public int interval { get; set; }
        public int count { get; set; }
    }
}

my controller:

public ActionResult Create(Parkim parkim, DateTime ioentry, DateTime iodeparture)
        {
                using (var context = new ParkimEntities())
                {
                    var test = db.Parkims.Where(x => x.departure != null && ioentry >=         
  x.entry && iodeparture <= x.departure).Select(s => new
                    {
                        Transdate = s.entry,
                        JobTime = DbFunctions.DiffHours(s.departure, s.entry)
                    }).ToList();



                    var result = (from t in test
                                  group t by new { t.Transdate, t.JobTime } into grp
                                  select new raporte
                                  {
                                      hyrje = grp.Key.Transdate,
                                      interval = grp.Key.JobTime??0,
                                      count = grp.Count()
                                  }).ToList();

                return RedirectToAction("Index", "Raporte", result);
             
            }
        }

ioentry and io departure are taken from the user before he is redirected to the index view:

@model IEnumerable<Parkim_Task.Models.raporte>

@{
    ViewBag.Title = "Parkim";
}

<h2>Raporte</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Vendos kohen e hyrjes</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


        <div class="form-group">
            <label>Koha e hyrjes</label>
            <input type="datetime-local" name="ioentry" placeholder="car entry" />
            <br />
            <label>Koha e daljes</label>
            <input type="datetime-local" name="iodeparture" placeholder="car departure"     />

       
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Prano" class="btn btn-default" />
            </div>
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Now im trying to display the intervals in which users have parked their cars grouped according to my controller but i cant seem to display any of the values of my model:

@model IEnumerable<Parkim_Task.Models.raporte>

@{
    ViewBag.Title = "Index";
}

<head>
    <title>Title of the document</title>
</head>

<body>

    @if (Model != null)
    {


        foreach (var spot in Model)
        {

            <p>@spot.interval;</p>

        }
    }

</body>

This returns a blank page. What am i doing wrong? How can i display the values of my model ive tried to group according to the controller?

1 Answers

You can try to use TempData to pass data to view:

public ActionResult Create(Parkim parkim, DateTime ioentry, DateTime iodeparture)
        {
                using (var context = new ParkimEntities())
                {
                    var test = db.Parkims.Where(x => x.departure != null && ioentry >=         
  x.entry && iodeparture <= x.departure).Select(s => new
                    {
                        Transdate = s.entry,
                        JobTime = DbFunctions.DiffHours(s.departure, s.entry)
                    }).ToList();



                    var result = (from t in test
                                  group t by new { t.Transdate, t.JobTime } into grp
                                  select new raporte
                                  {
                                      hyrje = grp.Key.Transdate,
                                      interval = grp.Key.JobTime??0,
                                      count = grp.Count()
                                  }).ToList();
                TempData["raportes"] = result;
                return RedirectToAction("Index", "Raporte");
             
            }
        }

view:

@model IEnumerable<Parkim_Task.Models.raporte>

@{
    ViewBag.Title = "Index";
}

<head>
    <title>Title of the document</title>
</head>

<body>

    @if (TempData.Peek("raportes") != null)
    {
        foreach (var spot in TempData["raportes"] as List<Parkim_Task.Models.raporte>)
        {

            <p>@spot.interval</p>

        }

    }

</body>
Related