I have 3 tables: 1)Agents 2)Agent Queue 3)Agent Nodes. The AgentId is a primary key in 1) and the tables 2) & 3) are dependent on 1. I want to create a model to access properties of these tables for one view. I am new to ASP.NET CORE and it would be great if anyone could point me to the right direction to do the same.
My Model Classes are:
public class Agents
{
[Key]
[DisplayName("Agent Id")]
public long AgentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Status { get; set; }
[DisplayName("Last Checked")]
public DateTime LastChecked { get; set; }
public bool Deleted { get; set; }
public IEnumerable<Agents> agents { get; set; }
public IEnumerable<AgentMachines> agentMachines { get; set; }
public IEnumerable<AgentQueues> agentQueues { get; set; }
}
public class AgentMachines
{
[Key]
[DisplayName("Machine Id")]
public long MachineId { get; set; }
[DisplayName("Agent Id")]
public long AgentId { get; set; }
public string Address { get; set; }
public string IP { get; set; }
[DisplayName("Logged User")]
public string LoggedUser { get; set; }
[DisplayName("AD User Account")]
public string ADUserAccount { get; set; }
public IEnumerable<Agents> agents { get; set; }
}
public class AgentQueues
{
[Key]
[DisplayName("Agent Queue Id")]
public long AgentQueueId { get; set; }
[DisplayName("Agent Id")]
public long AgentId { get; set; }
[DisplayName("Test Id")]
public long TestId { get; set; }
[DisplayName("Value Set Id")]
public long ValueSetId { get; set; }
[DisplayName("Process Id")]
public string ProcessId { get; set; }
public string Status { get; set; }
[DisplayName("Update User")]
public string UpdateUser { get; set; }
[DisplayName("Schedule Time")]
public DateTime? ScheduleTime { get; set; }
[DisplayName("Update Date")]
public DateTime? UpdateDate { get; set; }
public bool Deleted { get; set; }
[DisplayName("Test")]
public string TestName { get; set; }
[DisplayName("Value Set")]
public string ValueSetName { get; set; }
public IEnumerable<Agents> agents { get; set; }
}
public class TestAgentDetail
{
public IEnumerable<AgentMachines> agentMachines { get; set; }
public IEnumerable<AgentQueues> agentQueues { get; set; }
public IEnumerable<Agents> agents { get; set; }
}
The controller code I have so far is:
public async Task<IActionResult> Details(long? id)
{
var viewModel = new TestAgentDetail();
viewModel.agents = await _context.Agents
.Include(i => i.agentMachines)
.Include(i => i.agentQueues)
.AsNoTracking()
.ToListAsync();
if (id != null)
{
Agents agent = viewModel.agents.Where(
i => i.AgentId == id.Value).Single();
viewModel.agentMachines = agent.agentMachines.Select(s => s.LoggedUser);
}
return View(viewModel);
}
