我有一個漂亮的基本/標準的MVC項目,我正在建設。在它一切正常(圖1),直到我添加了一個DateTime對象,我的模型(圖2),在該點查看該模型變得非常扭曲的,並不會顯示任何信息(圖3)。查看我的SQL數據庫中有關相關日期時間對象的條目(圖4)。DateTime破壞視圖
我只是假設它是專門用於DateTime數據類型的東西,但我不確定是什麼原因導致了這個問題,或者我可以嘗試哪些解決方法。
圖1:
圖2:
圖3:
圖4:
從我的瀏覽守則問:
@model TKOPOC.Models.SubmitEIDs
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@* Model Update Alert *@
@if(Model.isVerify){ <h1><span style="color: red;">VERIFY YOUR CHANGES, THEN CLICK SUBMIT</span></h1> }
@*Search Box*@
@if(!Model.isVerify)
{
using (Html.BeginForm("Index", "EightID", FormMethod.Get))
{
<p>
Find by name or EID: @Html.TextBox("searchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
}
<table>
<tr>
<th>
@Html.ActionLink("EID", "Index", new { sortOrder=ViewBag.EIDSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Name", "Index", new { sortOrder=ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Email", "Index", new { sortOrder=ViewBag.EmailSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Physical", "Index", new { sortOrder=ViewBag.PhysicalSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Admin", "Index", new { sortOrder=ViewBag.AdminSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink(" Maint", "Index", new { sortOrder=ViewBag.MaintSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink(" Cab", "Index", new { sortOrder=ViewBag.CabSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink(" Mills", "Index", new { sortOrder=ViewBag.MillSortParam, currentFilter=ViewBag.CurrentFilter })
</th>
<th></th>
</tr>
@foreach (var item in Model.EightIDsPagedList) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Physical)
</td>
<td>
@Html.DisplayFor(modelItem => item.Admin)
</td>
<td>
@Html.DisplayFor(modelItem => item.Maint)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cab)
</td>
<td>
@if (item.Admin | item.Maint)
{
<text>All</text>
}
else
{
@Html.DisplayFor(modelItem => item.Mills.Count)
}
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EightIDID }) |
@Html.ActionLink("Details", "Details", new { id=item.EightIDID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EightIDID })
</td>
</tr>
}
</table>
<p> </p>
@* Paging *@
<div>
Page @(Model.EightIDsPagedList.PageCount < Model.EightIDsPagedList.PageNumber ? 0 : Model.EightIDsPagedList.PageNumber)
of @Model.EightIDsPagedList.PageCount
@if (Model.EightIDsPagedList.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink("< Prev", "Index", new { page = Model.EightIDsPagedList.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@for (var x = 1; x < Model.EightIDsPagedList.PageCount; x++)
{
if (Model.EightIDsPagedList.PageNumber == x)
{
@x;
}
else
{
@Html.ActionLink(Convert.ToString(x), "Index", new { page = x, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter });
}
}
@if (Model.EightIDsPagedList.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new { page = Model.EightIDsPagedList.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { page = Model.EightIDsPagedList.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>
<p> </p>
<table border="0">
<tr>
@if (!Model.isVerify)
{
using (Html.BeginForm("Index", "EightID", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<td>
<input type="file" name="file" required />
<input type="submit" name="Verify" value="Verify" />
</td>
}
}
else
{
using (Html.BeginForm("Submit", "EightID", FormMethod.Post, new { NewEIDs = Model.EightIDs }))
{
<td>
<input type="submit" name="Submit Changes" value="Submit" />
</td>
}
using (Html.BeginForm("Cancel", "EightID", FormMethod.Post))
{
<td>
<input type="submit" name="Cancel" value="Cancel" />
</td>
}
}
</td>
</tr>
</table>
編輯:
通過觀察一些調試後,我發現其中的問題時,只是沒有爲什麼 - 在表頭之後,我們回到DisplayFor發生的foreach()循環。這裏,該程序符合foreach。然後Model.EightIDsPagedList。然後「進入」。但是它不會進入var或item,只是完全跳過循環。當我刪除有問題的DateTime項目時,程序按照您預期的那樣進入循環,並繼續執行。
如果使用剃鬚刀,您應該包含您的視圖代碼,一個'.cshtml'文件。 – Igor
做喲你有一個DateTime的列?它看起來像是讓它的項目爲零,但沒有列標題。 「{0:dd/MM/yyyy}」 – jdweng
我並不完全熟悉格式化強制,但足以說這個問題發生在我添加該部分之前。我的印象是,0與格式化的方式有關,而不是與列分配聽起來像什麼有關。但是,如果你詢問現有的列是否在數據庫中,那麼是的,有一列與第一行的條目 –