I have a CalendarioJuego model that has a foreign key of the FechaPartido model and in turn this has a foreign key of the Temporada model. How can I access the attribute "Nombre" of the Temporada model from the CalendarioJuegoViewModel model to be able to display the associated details in a Index view and not showing just the Ids. How can I do that?
CalendarioJuego Model in API
public partial class CalendarioJuego
{
public int CalJuegoId { get; set; }
public int? CalFechaPartido { get; set; }
public int? CalEquipoLocal { get; set; }
public int? CalEquipoVisitante { get; set; }
public virtual Equipo? CalEquipoLocalNavigation { get; set; }
public virtual Equipo? CalEquipoVisitanteNavigation { get; set; }
public virtual FechaPartido? CalFechaPartidoNavigation { get; set; }
public virtual ICollection<Partido> Partidos { get; set; } = new List<Partido>();
}
CalendarioJuego MVC consumer
public class CalendarioJuego
{
[DisplayName("Id")]
public int CalJuegoId { get; set; }
[DisplayName("Equipo Local")]
public int CalEquipoLocal { get; set; }
[DisplayName("Equipo Visitante")]
public int CalEquipoVisitante { get; set; }
[DisplayName("Fecha de partido")]
public int CalFechaPartido { get; set; }
}
CalendarioJuegoViewModel
public class CalendarioJuegoViewModel
{
[DisplayName("Id")]
public int CalJuegoId { get; set; }
[DisplayName("Fecha de partido")]
public DateTime CalFechaPartido { get; set; }
[DisplayName("Equipo Local")]
public string CalEquipoLocal { get; set; }
[DisplayName("Equipo visitante")]
public string CalEquipoVisitante { get; set; }
[DisplayName("Hora")]
public TimeSpan FecHora { get; set; }
}
FechaPartido MVC consumer
public class FechaPartido
{
public int FecId { get; set; }
[DisplayName("Fecha de partido")]
public DateTime FecFechaPartido { get; set; }
[DisplayName("Temporada")]
public int FecTemporada { get; set; }
[DisplayName("Hora")]
public TimeSpan FecHora { get; set; }
}
FechaPartidoViewModel MVC consumer
public class FechaPartido
{
public int FecId { get; set; }
[DisplayName("Fecha de partido")]
public DateTime FecFechaPartido { get; set; }
[DisplayName("Temporada")]
public string FecTemporada { get; set; }
[DisplayName("Hora")]
public TimeSpan FecHora { get; set; }
}
Temporada MVC consumer
public class FechaPartido
{
[DisplayName("Id")]
public int TemId { get; set; }
[DisplayName("Nombre")]
public string TemNombre { get; set; } = null!;
[DisplayName("Fecha de inicio")]
public DateTime TemFecInicio { get; set; }
[DisplayName("Fecha de termino")]
public DateTime TemFecFinal { get; set; }
}
}
CalendarioJuegoController MVC consumer
public ActionResult Index()
{
string baseApiUrl = _configuration.GetSection("LigaDominicanaApi").Value!;
List<CalendarioJuego> calendarioJuegos = new List<CalendarioJuego>();
List<FechaPartido> fechapartidos = new List<FechaPartido>();
List<Equipo> equipos = new List<Equipo>();
List<CalendarioJuegoViewModel> calendarioJuegoViewModel = new List<CalendarioJuegoViewModel>();
string jsonCalendarioJuegosResponse = httpClient.GetStringAsync($"{baseApiUrl}/calendariojuegos").Result;
calendarioJuegos = string.IsNullOrEmpty(jsonCalendarioJuegosResponse) ? calendarioJuegos : JsonConvert.DeserializeObject<List<CalendarioJuego>>(jsonCalendarioJuegosResponse)!;
string jsonfechapartidosResponse = httpClient.GetStringAsync($"{baseApiUrl}/fechapartidos").Result;
fechapartidos = string.IsNullOrEmpty(jsonfechapartidosResponse) ? fechapartidos : JsonConvert.DeserializeObject<List<FechaPartido>>(jsonfechapartidosResponse)!;
string jsonEquiposResponse = httpClient.GetStringAsync($"{baseApiUrl}/equipos").Result;
equipos = string.IsNullOrEmpty(jsonEquiposResponse) ? equipos : JsonConvert.DeserializeObject<List<Equipo>>(jsonEquiposResponse)!;
foreach (CalendarioJuego juego in calendarioJuegos)
{
CalendarioJuegoViewModel customCalendarioJuego = new CalendarioJuegoViewModel()
{
CalJuegoId = juego.CalJuegoId,
CalEquipoLocal = equipos.Where(e => juego.CalEquipoLocal == e.EqId).Select(e => e.EqNombre).FirstOrDefault()!,
CalEquipoVisitante = equipos.Where(e => juego.CalEquipoVisitante == e.EqId).Select(e => e.EqNombre).FirstOrDefault()!,
CalFechaPartido = fechapartidos.Where(e => juego.CalFechaPartido == e.FecId).Select(e => e.FecFechaPartido).FirstOrDefault()!,
FecHora = fechapartidos.Where(e => juego.CalFechaPartido == e.FecId).Select(e => e.FecHora).FirstOrDefault()!,
};
calendarioJuegoViewModel.Add(customCalendarioJuego);
};
return View(calendarioJuegoViewModel);
}
How can I access the attribute "Nombre" of the Temporada model from the CalendarioJuegoViewModel model to be able to display the associated details in a Index view and not showing just the Ids. How can I do that?