【问题标题】:ASP.NET MVC view not displaying correct info from databaseASP.NET MVC 视图未显示来自数据库的正确信息
【发布时间】:2015-08-13 12:17:39
【问题描述】:

所以我正在构建要在我们的网站上显示的报告,我为 qry 创建了一个视图并将其放入我们的 .EDMX 文件中。我可以使用该查询为站点设置视图,但是数据库中的数据与屏幕上显示的数据不匹配。日期搞砸了,其他一切都匹配。有什么想法吗?

SELECT TOP 1000 [Name]
  ,[Manufacturer]
  ,[SerialNumber]
  ,[TSIFBarcode]
  ,[NextCalibrationDate]
  ,[Description]
  ,[CalibrationCost]
  ,[Location]
  ,[RoutineInspection]
  ,[Comment]
  ,[Priority]
FROM [InsertDBHere]

这是我的 sql 代码,下面是我从控制器创建视图的代码:

IEnumerable<qryCalibrationEquipmentTracker> view = db.qryCalibrationEquipmentTrackers;
view = view.OrderBy(a => a.NextCalibrationDate);
return View(view);

下面是视图代码

@model IEnumerable<MvcDBFirst.Models.qryCalibrationEquipmentTracker>
@{    
     ViewBag.Title = "Calibration Tracking Report";
 }
<h2>Calibration Tracking Report</h2>
@if (ViewBag.PrintMode == null || !ViewBag.PrintMode)
{
<div class="float-right clear-fix">
    <div class="amMenu">
        <ul class="amMenuLinksHoriz clear-fix">
            <li>
                <a href="@Url.Action("ExportCalTracking", "Reports")">
                    <img src="@Url.Content("~/images/assetmgr/printer.png")" height="25px" alt="Printer" />
                </a>
            </li>
            <li>
                @*<a href="@Url.Action("ExcelCalTracking", "Reports")">*@
                <a onclick="postTableData('amCalIndexTable')">
                    <img src="@Url.Content("~/images/assetmgr/excel.png")" height="25px" alt="Excel" />
                </a>
            </li>
        </ul>
    </div>
</div>
}


<h2>Report Run on @DateTime.Now.ToString()</h2>
   <table class="amCalIndexTable">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Manufacturer)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SerialNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TSIFBarcode)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NextCalibrationDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Priority)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CalibrationCost)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Location)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RoutineInspection)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Comment)
        </th>
    </tr>
</thead>
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Manufacturer)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SerialNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TSIFBarcode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NextCalibrationDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Priority)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
                @*Html.ActionLink(item.Description, "Details", "tblItems",
                        new
                        {
                            type = item.fkItemType,
                            serialNumber = item.SerialNumber,
                            owner = item.fkOwner
                        }, null
                    )*@
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CalibrationCost)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Location)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RoutineInspection)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Comment)
            </td>

        </tr>
    }
</tbody>

【问题讨论】:

  • 日期如何“搞砸”?我们无法从这里看到您的屏幕,您需要指定问题所在。显示的代码没有任何问题。
  • 所以这是来自数据库2013-05-14 00:00:00 2013-10-07 00:00:00 2014-11-11 00:00:00 NULL 2015-11-05 00:00:00 的一些日期,这些日期是正确的,但在视图中它们只会出现在所有 5 个 2015-10-07
  • 显示 MVC 视图的代码
  • @Neal:发布的代码中没有任何内容会导致这种情况。
  • 我已添加查看代码

标签: c# sql asp.net asp.net-mvc


【解决方案1】:

像这样将您的view 转换为OrderBy 之前的列表。 view.ToList().OrderBy().

像这样。

IEnumerable<qryCalibrationEquipmentTracker> view = db.qryCalibrationEquipmentTrackers;
view = view.ToList().OrderBy(a => a.NextCalibrationDate);
return View(view);

【讨论】:

  • 他们仍然有相同的日期
【解决方案2】:

这是一个 Microsoft 问题,因为它是一个没有主键的查询。微软进来并随机选择了一个主键。我们已经更新了我们的查询来处理这个问题。

【讨论】:

  • 很抱歉,如果我必须在这里打赌,问题可能出在您的代码中,而不是微软的...
  • 这里的其他人都在说代码是正确的。在做了我所说的之后,它起作用了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多