I'm trying to figure out what's wrong here. Is not very complex but I was sure I had everything in order.
I have two models, Machines and Devices. I'm constructing a table for theDevice where, in one column, I show a property of the related Machine.
Here is the declaration of the relation between these two models:
public class Device { [Key] public int DeviceID { get; set; } public int Id { get; set; } public Machine Machines { get; set; } public DeviceStatus DeviceStatus { get; set; } } public class Machine { public int Id { get; set; } [Display(Name = "Modelo")] public int TypeID { get; set; } [Display(Name = "Serial")] public string MchName { get; set; } [Display(Name = "Tienda")] public int StoreID { get; set; } public virtual ICollection<Device> Devices { get; set; } public Store Stores { get; set; } }
Here is the Get Method for the Device model. Note: This code is inside the MachineController since I won't be doing much with the Devices beyond this list
public async Task<IActionResult> Contadores(/*int? id*/) { var listacontadores = _context.Devices.Include(i => i.Machines).AsNoTracking(); return View(await listacontadores.ToListAsync()); }
And this is the View:
@model IEnumerable<Application.Models.Device><table id="contadores" class="table table-bordered table-hover table-striped" ><thead><tr><th class="col-md-2">ID Contador</th><th class="col-md-2">@Html.DisplayNameFor(model=>model.Machines) </th></tr></thead><tbody> @foreach (var item in Model) {<td class="col-md-3"><input form="@(String.Format("{0}{1}","form",item.DeviceID))" type="hidden" asp-for="@item.DeviceID" /><div class="form-group" form="@(String.Format("{0}{1}","form",item.DeviceID))"><div><input form="@(String.Format("{0}{1}","form",item.DeviceID))" asp-for="@item.DeviceName" name="DeviceName" readonly class="form-control" /><span asp-validation-for="@item.DeviceName" class="text-danger"></span></div></div></td><td class="col-md-2"> @Html.DisplayFor(modelItem => item.Machines.Id)</td>
About these lines:
1) In the header
<th class="col-md-2">@Html.DisplayNameFor(model=>model.Machines) </th>
The Problem
2) The problem is in the body. This line:
@Html.DisplayFor(modelItem => item.Machines.Id)
This is showing a blank space in the table. Nothing.
Anything else I should check? Thanks in advance.