【问题标题】:Problem with displaying data from the linq query显示来自 linq 查询的数据的问题
【发布时间】:2019-01-16 12:32:08
【问题描述】:

我尝试使用 linq 显示两个表中的数据,没有错误,但我认为我的数据没有到达视图。在调试代码时,我看到 linq 处理了正确的信息。运行我的应用程序后,没有数据出现,只有@Html.DisplayNameFor 出现。

var details = from l in db.LogDetails.ToList()
              join a in db.AuditLog.ToList()
                 on l.AuditLogId equals a.AuditLogId into b
              from d in b.DefaultIfEmpty()
              where d.RecordId.Equals(id)
              select new AuditLogDetail
                      {
                          PropertyName = l.PropertyName,
                          OriginalValue = l.OriginalValue,
                          NewValue = l.NewValue
                      };

return View(details.ToList());

全景

@model IEnumerable<TrackerEnabledDbContext.Common.Models.AuditLogDetail>

@{
    ViewBag.Title = "Lista zgłoszeń";
}

<h2>Lista zgłoszeń</h2>

<p>
    @Html.ActionLink("Nowe zgłoszenie", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.PropertyName)

        </th>
        <th>
            @Html.DisplayNameFor(model => model.OriginalValue)

        </th>
        <th>
            @Html.DisplayNameFor(model => model.NewValue)
        </th>

    </tr>

    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PropertyName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OriginalValue)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NewValue)
        </td>
    </tr> 
    }

</table>

Screenshot of information that show up after debugging of return View(details.ToList());

我将 linq 查询更改为:

var r = from s in db.LogDetails
        select s;

并且表格中的每个值都正确显示,但是我需要根据另一个表格过滤数据,所以当我尝试以下代码时,出现错误:

无法创建“System.Object”类型的常量值。此上下文仅支持原始类型或枚举类型。

代码:

var r = from s in db.LogDetails
        join l in db.AuditLog
             on s.AuditLogId equals l.AuditLogId
        where l.RecordId.Equals(id)
        select s;

我已将equals更改为==,但它无法编译。

【问题讨论】:

  • 您是否在视图中将模型类型声明为@model IEnumerable&lt;AuditLogDetail&gt;
  • 是的模型已被声明。
  • 如果您的模型是 List 类型,那么 model =&gt; model.NewValue 将无法编译。能否贴出实际的视图代码,包括模型类型声明?
  • 我编辑了上面的帖子,请检查。
  • DisplayFor(modelItem 应该是DisplayFor(item

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


【解决方案1】:

尝试将 .Equals 替换为 ==

    var r = from s in db.LogDetails
    join l in db.AuditLog
         on s.AuditLogId equals l.AuditLogId
    where l.RecordId == id
    select s;

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多