ASP.NET MVC视图不显示数据库中的正确信息

时间:2015-08-13 12:17:40

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

所以我正在构建一个要在我们网站上显示的报告,我为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>

2 个答案:

答案 0 :(得分:0)

view转换为OrderBy之前的列表。 view.ToList().OrderBy()

喜欢这个。

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

答案 1 :(得分:0)

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