有三种型号:
承包商:
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:
我理解如何在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);
}