The Presentation Layer (capa_presentacion) is responsible for handling HTTP requests, user input, and rendering views. Built with ASP.NET Core MVC, this layer serves as the entry point for all user interactions with the application.
Manages soccer field operations. From capa_presentacion/Controllers/CanchasController.cs:
using Microsoft.AspNetCore.Mvc;using capa_entidad;using capa_negocio;namespace capa_presentacion.Controllers{ public class CanchasController : Controller { CN_Canchas Canchas = new CN_Canchas(); public ActionResult ListarCanchas() { try { if (!ModelState.IsValid) { throw new Exception("El estado del modelo no es válido."); } var olista = Canchas.ListarCanchas(); return View(olista); } catch (Exception ex) { TempData["ErrorMessage"] = "Error al obtener la lista de canchas: " + ex.Message; return View(new List<CE_Canchas>()); } } }}
public ActionResult ListarCanchas(){ try { if (!ModelState.IsValid) { throw new Exception("El estado del modelo no es válido."); } var olista = Canchas.ListarCanchas(); return View(olista); } catch (Exception ex) { TempData["ErrorMessage"] = "Error al obtener la lista de canchas: " + ex.Message; return View(new List<CE_Canchas>()); }}
The controller calls Canchas.ListarCanchas() from the business layer and passes the result to the view.
[HttpGet]public ActionResult Actualizar(int id){ var lista = Canchas.ListarCanchas(); var cancha = lista.FirstOrDefault(c => c.IdCancha == id); if (cancha == null) { return NotFound($"no se puedo actualizar la cancha con el id: {id}"); } return View(cancha);}
Manages client operations with similar CRUD patterns. From capa_presentacion/Controllers/ClientesController.cs:
using Microsoft.AspNetCore.Mvc;using capa_negocio;using capa_entidad;namespace capa_presentacion.Controllers{ public class ClientesController : Controller { CN_Clientes Clientes = new CN_Clientes(); public ActionResult ListarClientes() { try { if (!ModelState.IsValid) { throw new Exception("El estado del modelo no es válido."); } var olista = Clientes.Listar(); return View(olista); } catch (Exception ex) { TempData["ErrorMessage"] = "Error al obtener la lista de clientes: " + ex.Message; return View(new List<CE_Clientes>()); } } // Additional CRUD methods... }}
Reservations use a ViewModel to combine multiple data sources. From ReservasController.cs:35-50:
public ActionResult InsertarReservas(){ // Create the ViewModel package ReservaViewModel modelo = new ReservaViewModel(); // Load dropdown data from multiple business layer classes modelo.ListaClientes = new CN_Clientes().Listar(); modelo.ListaCanchas = new CN_Canchas().ListarCanchas(); // Initialize the reservation entity modelo.Reserva = new CE_Reservas(); // Pass the complete model to the view return View(modelo);}
The ViewModel contains:
Reserva: The main reservation entity
ListaClientes: Available clients for dropdown
ListaCanchas: Available soccer fields for dropdown
return StatusCode(404, "No se encontró el modelo");return StatusCode(500, $"Error: {ex.Message}");return NotFound($"Cliente con id {id} no encontrado");
try{ if (!ModelState.IsValid) { throw new Exception("El estado del modelo no es válido."); } var olista = Canchas.ListarCanchas(); return View(olista);}catch (Exception ex){ TempData["ErrorMessage"] = "Error al obtener la lista: " + ex.Message; return View(new List<CE_Canchas>());}
public class ReservaViewModel{ public CE_Reservas Reserva { get; set; } public List<CE_Clientes> ListaClientes { get; set; } public List<CE_Canchas> ListaCanchas { get; set; }}
Use ViewModels when:
A view needs data from multiple entities
The view needs different data than the entity provides
You need to combine entity data with UI-specific data
For update operations, retrieve the existing entity:
public ActionResult Actualizar(int id){ var lista = Canchas.ListarCanchas(); var cancha = lista.FirstOrDefault(c => c.IdCancha == id); if (cancha == null) { return NotFound($"Cancha con id {id} no encontrada"); } return View(cancha);}