DateTime会破坏View

时间:2016-03-30 19:04:35

标签: c# sql asp.net-mvc

我正在构建一个非常基本/标准的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>

编辑:

通过视图进行了一些调试之后,我发现 where 出现了问题,而不是为什么 - 在表格标题之后,我们来到了foreach( )DisplayFor发生的循环。在这里,该计划击中了foreach。然后是Model.EightIDsPagedList。然后“在”。但是它永远不会进入var或item,只是完全跳过循环。当我删除有问题的DateTime项目时,程序按照您的预期进入循环并继续执行。

1 个答案:

答案 0 :(得分:1)

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

  1. 在代码中使DateTime?(可为空的DateTime)。
  2. 确保您在SQL中有值
  3. 使SQL列不可为空(这将保证选项2)。
  4. 如果有帮助,请告诉我们。