using Control_de_datos___KPI.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; namespace Control_de_datos___KPI.Pages.Clientes { public class IndexModel : PageModel { private readonly SistemaKPIContext _context; public IndexModel(SistemaKPIContext context) { _context = context; } [BindProperty] public Cliente NuevoCliente { get; set; } = new(); public List ListaClientes { get; set; } = new(); public async Task CargarListasAsync() { ListaClientes = await _context.Clientes.ToListAsync(); } public async Task OnGetAsync() { await CargarListasAsync(); } public async Task OnPostAsync() { if (!ModelState.IsValid) { await CargarListasAsync(); return Page(); } _context.Clientes.Add(NuevoCliente); await _context.SaveChangesAsync(); return RedirectToPage(); } public async Task OnPostEliminarAsync(int id) { var cliente = await _context.Clientes.FindAsync(id); if (cliente != null) { _context.Clientes.Remove(cliente); await _context.SaveChangesAsync(); } return RedirectToPage(); } } }