Я хочу отобразить имя свойства поля в представлении (отчете). Модель:
public class Report
{
[Display(Name ="Total Attendance")]
public int Attendance { get; set; }
[Display(Name = "Total Offering")]
[DataType(DataType.Currency)]
public double Amount { get; set; }
[Display(Name = "District Name")]
public int LocationId { get; set; }
[ForeignKey("LocationId")]
public Location Location { get; set; }
[Display(Name = "Weekly Service")]
public int WeeklyServiceId { get; set; }
[ForeignKey("WeeklyServiceId")]
[Display(Name = "Weekly Service")]
public WeeklyService WeeklyService { get; set; }
public DateTime? Sdate { get; set; }
public DateTime? Edate { get; set; }
public string UsherName { get; set; }
public string LocationName { get; set; }
}
Контроллер:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Report(Report model)
{
var startDate = model.Sdate;
var endDate = model.Edate;
var QueryResult = (from pay in _context.PaymentRecords.Include(p=>p.Location).Include(p=>p.WeeklyService)
//join a in _context.Locations on pay.LocationId equals a.Id
//join c in _context.WeeklyServices on pay.WeeklyServiceId equals c.Id
where (pay.DepositDate.Date >= startDate)
where (pay.DepositDate.Date <= endDate)
group pay by new { pay.LocationId,pay.WeeklyServiceId} into g
orderby g.Key.LocationId
select new Report
{
LocationId= g.Key.LocationId,
Attendance = g.Sum(x => x.Attendance),
Amount = g.Sum(x => x.Amount),
WeeklyServiceId =g.Key.WeeklyServiceId
});
return View("Report", QueryResult);
}
Просмотр / Отчет
<table class="table table-striped table-bordered" id="myTable">
<thead class="thead-dark">
<tr>
<th>SN</th>
<th>@Html.DisplayNameFor(model => model.Location)</th>
<th>@Html.DisplayNameFor(model => model.Attendance)</th>
<th>@Html.DisplayNameFor(model => model.Amount)</th>
<th>@Html.DisplayNameFor(model => model.WeeklyService)</th>
</tr>
</thead>
<tbody>
@if (Model.Count() > 0)
{
int c = 0;
foreach (var item in Model)
{
c++;
<tr>
<td>@c</td>
<td>@Html.DisplayFor(modelItem => item.Location.Name)</td>
<td>@Html.DisplayFor(modelItem=>item.Attendance)</td>
<td>@item.Amount.ToString("C")</td>
<td>@Html.DisplayFor(modelItem=>item.WeeklyService.Name)</td>
</tr>
}
}
else
{
}
</tbody>
</table>
Результат вышеперечисленного
Обратите внимание, что Location - это модель, в которой свойство Name имеет свойство, а также WeeklyService. Но если я изменю данные таблицы на LocationId и WeeklyServiceId, он отобразит результаты с идентификатором. Но я хочу, чтобы он отображал имя местоположения и WeeklyService вместо их идентификаторов. ![enter image description here](https://i.stack.imgur.com/upqFN.jpg)