There are three models:
Contractor:
public class Contractor
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ReportWork> ReportWorks { get; set; }
}
Report:
public class Report
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ReportWork> ReportWorks { get; set; }
}
Report Work:
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; }
}
How to generate the JsonResult of the following table:
- Name Contractor Report.Name1 Report.Name2
- Contractor.Name1 ReportWork.ReportStatus ReportWork.ReportStatus
- Contractor.Name2 Not ReportWork.ReportStatus
- Contractor.Name3 ReportWork.ReportStatus Not
I understand how to do this in 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>
But how to do the same in Json in effect, form a table with N number of columns.
[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);
}