如何生成交叉表{J}的Json结果

时间:2018-02-19 10:21:22

标签: c# asp.net json

有三种型号:

承包商:

public class Contractor
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<ReportWork> ReportWorks { get; set; }
    }

报告:

public class Report
    {
        public int Id { get; set; }       
        public string Name { get; set; }        
        public ICollection<ReportWork> ReportWorks { get; set; }
    }

报告工作:

public class ReportWork
    {
        public int Id { get; set; }
        public string ReportStatus { get; set; }        
        public int ReportId { get; set; }
        public virtual Report Reports { get; set; }
        public int ContractorId { get; set; }
        public virtual Contractor Contractors { get; set; }        
    }

如何生成下表的JsonResult:

  • 名称承包商Report.Name1 Report.Name2
  • Contractor.Name1 ReportWork.ReportStatus ReportWork.ReportStatus
  • Contractor.Name2 Not ReportWork.ReportStatus
  • Contractor.Name3 ReportWork.ReportStatus Not

我理解如何在Razor中执行此操作:

<table>
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>

            @foreach (var it in ViewBag.Report)
            {
            <th>
                @it.Name
            </th>
            }
        </tr>
    </thead>

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <th>
                    @Html.DisplayFor(modelItem => item.Name)
                </th>

                @foreach (var it in ViewBag.Report)
                {
                    @if (item.ReportWorks.SingleOrDefault(c => c.ReportId == it.Id) != null)
                    {
                        <th>
                            @item.ReportWorks.SingleOrDefault(c => c.ReportId == it.Id).ReportStatus
                        </th>
                    }
                    else
                    {
                        <th>
                            Not
                        </th>
                    }
                }
            </tr>
        }
    </tbody>
</table>

但是如何在Json中做同样的事情 实际上,形成一个包含N列的表。

[HttpGet]
        public JsonResult ()
        {
             var events = from e in _context.Contractor
                         from p in _context.ReportWork
                         select new
                         {
                             id = e.Id,
                             name = e.Name,
                              // Add here columns with N number of reports
                         };
            var rows = events.ToArray();

            return Json(rows);
        }

0 个答案:

没有答案