我有一个相当基础/标准的MVC项目正在构建中。一切都很正常(图1),但是当我向我的模型中添加DateTime对象(图2)时,与该模型相关的视图会变得非常扭曲,并且不会显示任何信息(图3)。请参见SQL数据库中相关DateTime对象的条目(图4)。
我认为这可能与DateTime数据类型有关,但我不确定是什么原因导致了这种情况,或者应该尝试哪些故障排除方法。
图1:
我认为这可能与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>
编辑:
经过对视图的进一步调试,我发现了问题出现的位置,但不知道原因——在表头之后,我们进入foreach()循环,其中发生了DisplayFor。在这里,程序进入foreach,然后进入Model.EightIDsPagedList,然后是“in”。但是它从未进入var或item,直接跳过整个循环。当我删除有问题的DateTime项时,程序按预期进入循环并继续执行。
.cshtml
文件。 - Igor