diff --git a/TripManager2.0/Controllers/EditorController.cs b/TripManager2.0/Controllers/EditorController.cs index 13d6a7a..58e6dc6 100644 --- a/TripManager2.0/Controllers/EditorController.cs +++ b/TripManager2.0/Controllers/EditorController.cs @@ -217,9 +217,10 @@ public IActionResult UpdateVisa(EditVisaViewModel vm) Name = vm.Nombre }; - if (vm.SelectedRegion != null || vm.SelectedRegion.Count() > 0) + if (vm.SelectedRegion != null && vm.SelectedRegion.Count() > 0) { var regiones = (IEnumerable)getter.GetAll("Region"); + var regiones_visas = (IEnumerable)getter.GetAll("Region_Visa"); List regiones_visa = new List(); foreach (var name in vm.SelectedRegion) @@ -227,16 +228,21 @@ public IActionResult UpdateVisa(EditVisaViewModel vm) { if (region.Nombre == name) { - regiones_visa.Add(new Region_Visa() { Region = region, Visa = toUpd }); + var old = regiones_visas.Where(rv => rv.Region.Nombre == name && rv.Visa.Name == toUpd.Name).SingleOrDefault(); + if (old != null) + regiones_visa.Add(old); + else + regiones_visa.Add(new Region_Visa() { Region = region, Visa = toUpd }); break; } } toUpd.Regiones = regiones_visa; } - if (vm.SelectedPais != null || vm.SelectedPais.Count() > 0) + if (vm.SelectedPais != null && vm.SelectedPais.Count() > 0) { var paises = (IEnumerable)getter.GetAll("Pais"); + var paises_visas = (IEnumerable)getter.GetAll("Pais_Visa"); List paises_visa = new List(); foreach (var name in vm.SelectedPais) @@ -244,14 +250,19 @@ public IActionResult UpdateVisa(EditVisaViewModel vm) { if (pais.Nombre == name) { - paises_visa.Add(new Pais_Visa() { Pais = pais, Visa = toUpd }); + var old = paises_visas.Where(pv => pv.Pais.Nombre == name && pv.Visa.Name == toUpd.Name).SingleOrDefault(); + if (old != null) + paises_visa.Add(old); + else + paises_visa.Add(new Pais_Visa() { Pais = pais, Visa = toUpd }); + break; } } toUpd.Paises = paises_visa; } service.UpdateVisa(entity, toUpd); - return RedirectToAction("Welcome"); + return RedirectToAction("EditVisa"); } } }