日期时间破坏了视图。

3
我有一个相当基础/标准的MVC项目正在构建中。一切都很正常(图1),但是当我向我的模型中添加DateTime对象(图2)时,与该模型相关的视图会变得非常扭曲,并且不会显示任何信息(图3)。请参见SQL数据库中相关DateTime对象的条目(图4)。
我认为这可能与DateTime数据类型有关,但我不确定是什么原因导致了这种情况,或者应该尝试哪些故障排除方法。
图1:

enter image description here

图2:

enter image description here

图3:

enter image description here

图4:

enter image description here

从我的视角编写的代码:

    @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>&nbsp</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>&nbsp</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项时,程序按预期进入循环并继续执行。


3
如果使用 Razor,您应该包含您的视图代码,即 .cshtml 文件。 - Igor
你有一个DateTime的列吗?看起来你把它作为第一项,但是这个列没有标题。"{0:dd/MM/yyyy}" - jdweng
我并不是很熟悉格式强制执行,但可以说这个问题在我添加那部分之前就已经出现了。我原本以为0与其格式有关,而不是像听起来是列赋值的问题?但如果你所询问的列在数据库中存在,那么是的,有一个该列,并且在第一行中有一个条目。 - Alex Watts
有趣的是,在SQL Server中,我将其设置为DateTime2(0),希望它能去除秒级精度(看到7表示7个秒位数,4表示4位精度等),但它仍然显示为yyyy-mm-dd 00:00:00.0000000。 - Alex Watts
此外,您还应该包含在填充模型的代码。 - Balde
1个回答

1

您是否在任何地方都遇到了空引用异常?当您运行示例时,在DateTime列中是否有值?您的SQL似乎是可空的,但您的属性不是。您可以尝试以下选项:

  1. 将其作为代码中的DateTime?(可空DateTime)。
  2. 确保您在SQL中有一个值
  3. 使SQL列不可为空(这将保证选项2)。

如果有帮助,请告诉我们。


让我在代码中将其设置为可空并进行检查。我想在数据库中使它可空,因为我正在测试环境下使用部分数据集,并希望避免手动填充100多个条目。至于任何异常,似乎根本没有出现任何异常-它似乎会忽略任何问题。 - Alex Watts
事实证明,将DateTime设置为可空使我取得了很大的进展。现在我只需要解决将mm/dd/yyyy HH:MM:SS AM/PM转换成mm/dd/yyyy的问题。最初我在它上面有一个装饰,就像上面的截图中看到的那样,但它好像不太喜欢这种格式转换,所以我会试着玩一下它。但就所有目的而言,它现在基本上已经工作了! - Alex Watts
1
嘿,我很高兴能帮到你。并非所有模型属性都可为空。在这种情况下,似乎是因为DateTime是一个结构体而不是类。由于C#中的结构体是值类型(与引用类型相对),它们默认情况下不可为空。如果您想使它们可为空,则必须显式地将它们设置为可空。您愿意接受我的答案吗?如果您仍然需要有关日期格式的帮助,可以发布一个新问题并在那里提供更多详细信息。 - Leon
回答被接受了 - 我在发布评论后不久就弄清楚了格式 :) - Alex Watts
谢谢!很棒,你也解决了日期格式的问题。继续保持! - Leon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接