[英]Cant iterate through model values in view mvc.net
我正在從事一個旨在將 function 作為停車應用程序的項目。 現在我正在嘗試創建一個報告,顯示在特定日期的特定時間停放了多少汽車。 為了在幫助下實現這一點,我有一個 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; }
}
}
我的 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 和 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")
}
現在我試圖顯示根據我的 controller 分組的用戶停放汽車的間隔,但我似乎無法顯示我的 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>
這將返回一個空白頁。 我究竟做錯了什么? 我如何顯示我的 model 的值,我試圖根據 controller 進行分組?
您可以嘗試使用 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<raporte>)
{
<p>@spot.interval</p>
}
}
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.