简体   繁体   中英

ASP.NET MVC view not displaying correct info from database

So I am building a report to be displayed on our website, I created a view for the qry and dropping it into our .EDMX file. I can set up the view for the site with that query however the data in the database is not matching up with what is being shown on screen. The dates are what is messing up, everything else matches. Any thoughts as to what is wrong?

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

That is my sql code, below is my code to create the view from my controller:

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

Below is code for the 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>

Convert your view to List before the OrderBy like this. view.ToList().OrderBy() .

Like this.

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

This was a Microsoft issue, since it was a query it didnt have a primary key. Microsoft came in and randomly chose a primary key. We have updated our query to deal with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM