2016-03-30 99 views
3

我有一个漂亮的基本/标准的MVC项目,我正在建设。在它一切正常(图1),直到我添加了一个DateTime对象,我的模型(图2),在该点查看该模型变得非常扭曲的,并不会显示任何信息(图3)。查看我的SQL数据库中有关相关日期时间对象的条目(图4)。DateTime破坏视图

我只是假设它是专门用于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> 

编辑:

通过观察一些调试后,我发现其中的问题时,只是没有为什么 - 在表头之后,我们回到DisplayFor发生的foreach()循环。这里,该程序符合foreach。然后Model.EightIDsPagedList。然后“进入”。但是它不会进入var或item,只是完全跳过循环。当我删除有问题的DateTime项目时,程序按照您预期的那样进入循环,并继续执行。

+3

如果使用剃须刀,您应该包含您的视图代码,一个'.cshtml'文件。 – Igor

+0

做哟你有一个DateTime的列?它看起来像是让它的项目为零,但没有列标题。 “{0:dd/MM/yyyy}” – jdweng

+0

我并不完全熟悉格式化强制,但足以说这个问题发生在我添加该部分之前。我的印象是,0与格式化的方式有关,而不是与列分配听起来像什么有关。但是,如果你询问现有的列是否在数据库中,那么是的,有一列与第一行的条目 –

回答

1

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

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

让我们知道是否有帮助。

+0

让我让它在代码中可以为空并检查。我想让它在数据库中为空,因为我正在测试环境中使用部分数据集,并且希望避免必须手工填充100多个条目。至于任何例外情况,我似乎没有得到任何 - 它只是吞下任何问题即将到来它似乎 –

+0

因此,事实证明,使DateTime为空可让我相当远。现在我只需要将mm/dd/yyyy HH:MM:SS AM/PM转换为mm/dd/yyyy。我原本在上面的屏幕截图中看到了它的装饰,但它似乎不太喜欢那种格式转换,所以我会玩弄它。但是,对于所有意图和目的,它现在基本上正在工作! –

+1

嘿,我很高兴,帮助。并非所有的模型属性都是可空的。在这种情况下,它似乎是由于DateTime是一个结构而不是一个类的事实。由于C#中的结构是值类型(与引用类型相对),它们默认情况下不可为空。如果你希望它们可以为空,你必须明确地使它们为空。谨慎接受我的回答呢?如果你仍然需要日期格式的帮助,你可以发布一个新的问题,并在那里提供更多的细节。 – Leon