5

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);
        }
blakcat
  • 634
  • 1
  • 7
  • 16
  • I am not quite sure what the problem is. Is it that you don't know how to create the query or how to serialize the result. Well... if it's the query, check this out: https://stackoverflow.com/questions/21051612/entity-framework-join-3-tables – Schadensbegrenzer Feb 19 '18 at 10:30
  • I don't understand the language of your code. What is it? It's defintely not C. – machine_1 Feb 19 '18 at 10:43
  • Yes it works if the number of reports is constant, but if it changes. In this and the sense that the collection should be dynamic. That is, today the table can have 5 columns, tomorrow 3, etc. – blakcat Feb 19 '18 at 10:47
  • Sorry for a tag error, this is C # – blakcat Feb 19 '18 at 10:48
  • 1
    Just return `new JsonResult(events)` – user743414 Feb 19 '18 at 10:52

0 Answers0