Overview
TheCanchasController handles all CRUD operations for sports fields (canchas) in the system. It provides endpoints for listing, creating, updating, and deleting field records.
Namespace: capa_presentacion.Controllers
Base Route: /Canchas
Methods
ListarCanchas
Retrieves a list of all sports fields in the system.HTTP Method
GET
Route
/Canchas/ListarCanchas
Returns
Type:ActionResult
- Success (200): Returns a view with
List<CE_Canchas>containing all sports fields - Error: Returns a view with an empty list and sets error message in
TempData["ErrorMessage"]
Response Model
Error Handling
- Validates
ModelStatebefore processing - Catches exceptions and stores error message in
TempData - Returns empty list on error instead of throwing
Usage Example
Source
Controllers/CanchasController.cs:17
AgregarCancha (GET)
Displays the form for adding a new sports field.HTTP Method
GET
Route
/Canchas/AgregarCancha
Returns
Type:ActionResult
Returns the view for creating a new cancha.
Usage Example
Source
Controllers/CanchasController.cs:43
AgregarCancha (POST)
Creates a new sports field in the system.HTTP Method
POST
Route
/Canchas/AgregarCancha
Parameters
The cancha object to createProperties:
Nombre(string): Name of the sports fieldTipo(string): Type of field (e.g., “Fútbol”, “Basketball”)PrecioPorHora(decimal): Hourly rental priceEstado(string): Current status of the field
Returns
Type:ActionResult
- Success (302): Redirects to
ListarCanchasaction - Error (404): Invalid model state - “No se encontro el modelo”
- Error (500): Exception during creation - “Error al agregar la cancha: “
Error Responses
Model state is invalid
Error during cancha creation
Usage Example
Source
Controllers/CanchasController.cs:54
Actualizar (GET)
Retrieves a specific sports field for editing.HTTP Method
GET
Route
/Canchas/Actualizar/{id}
Parameters
The unique identifier of the cancha to update
Returns
Type:ActionResult
- Success (200): Returns view with the
CE_Canchasobject - Error (404): Cancha not found - “no se puedo actualizar la cancha con el id: “
Error Responses
Cancha with specified ID not found
Usage Example
Source
Controllers/CanchasController.cs:79
Actualizar (POST)
Updates an existing sports field.HTTP Method
POST
Route
/Canchas/Actualizar
Parameters
The cancha object with updated informationProperties:
IdCancha(int): ID of the cancha to updateNombre(string): Updated nameTipo(string): Updated typePrecioPorHora(decimal): Updated priceEstado(string): Updated status
Returns
Type:ActionResult
- Success (302): Redirects to
ListarCanchasaction - Error (500): Exception during update - “Error al actualizar la cancha: “
Error Responses
Error during cancha update
Usage Example
Notes
- The method updates the cancha regardless of
ModelState.IsValidstatus (lines 99-105) - Both validation paths lead to the same update operation
Source
Controllers/CanchasController.cs:95
Eliminar
Deletes a sports field from the system.HTTP Method
POST
Route
/Canchas/Eliminar
Parameters
The unique identifier of the cancha to delete
Returns
Type:ActionResult
Success (302): Redirects to ListarCanchas action
Usage Example
Notes
- No error handling implemented
- Assumes the business layer handles validation
- Direct redirect after deletion
Source
Controllers/CanchasController.cs:121
Dependencies
- Business Layer:
CN_Canchasfromcapa_negocio - Entity Layer:
CE_Canchasfromcapa_entidad - Framework: ASP.NET Core MVC
Error Handling Strategy
- Model Validation: Checks
ModelState.IsValidbefore processing requests - Try-Catch Blocks: Wraps operations in exception handlers
- User Feedback: Uses
TempDatafor error messages in views - Status Codes: Returns appropriate HTTP status codes (404, 500)
- Graceful Degradation: Returns empty lists instead of error pages when listing fails