我正在开发一个旨在充当停车应用程序的项目。现在,我正在尝试创建一份报告,显示特定日期内特定时间停放的汽车数量。为了在帮助下实现这一目标,我有一个模型:
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; }
}
}
我的控制器:
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 和 io出发是在用户重定向到索引视图之前从用户那里获取的:
@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")
}
现在我尝试显示用户根据我的控制器分组停车的时间间隔,但我似乎无法显示模型的任何值:
@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>
这将返回一个空白页。我究竟做错了什么?如何显示我尝试根据控制器分组的模型的值?
可以尝试使用TempData来传递数据来查看:
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");
}
}
查看:
@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>