public IActionResult EditSoft(int id, int sample, SoftType softType, bool tissue, bool count, bool degree) { var softs = _context.Soft .Include(i => i.Individual) .Where(m => m.IndividualId == id && m.SoftType == softType); var feature = HttpContext.Features.Get <IRequestCultureFeature>(); var lang = feature.RequestCulture.Culture.TwoLetterISOLanguageName.ToUpperInvariant(); ViewData["lang"] = lang; var degrees = from Degree e in Enum.GetValues(typeof(Degree)) select new { Id = e, Name = e.GetDisplayName(lang) }; ViewData["Degree"] = new SelectList(degrees, "Id", "Name"); IndividualSoftTissueViewModel model = new IndividualSoftTissueViewModel() { Id = id, SamplingId = sample, SoftType = softType, Check = softs.Any(), }; model.Configs.AddRangeOverride(new Dictionary <string, bool> { { "tissue", tissue }, { "count", count }, { "degree", degree } }); if (tissue) { model.Tissues.AddRange(Enum.GetValues(typeof(Tissue)) .Cast <Tissue>() .Select(t => new TissueView { Check = softs.Any(s => s.Tissue == t), Count = softs.Any(s => s.Tissue == t) ? softs.FirstOrDefault(s => s.Tissue == t).Count : null, Degree = softs.Any(s => s.Tissue == t) ? softs.FirstOrDefault(s => s.Tissue == t).Degree : null, Text = t.GetDisplayName(lang), Value = ((int)t).ToString(CultureInfo.InvariantCulture), }).ToList()); } if (count && !tissue) { model.Count = softs.Any() ? softs.First().Count : null; } return(PartialView("_EditSoft", model)); }
public async Task <IActionResult> EditSoft([Bind("Id,SamplingId,SoftType,Tissues,Count,Configs,Check")] IndividualSoftTissueViewModel individual) { if (individual != null && ModelState.IsValid) { try { if (individual.Configs["tissue"]) { var softs = _context.Soft .Where(m => m.IndividualId == individual.Id && m.SoftType == individual.SoftType); if (individual.Configs["count"]) { var Options = Enum.GetValues(typeof(Tissue)) .Cast <Tissue>() .Select(t => new { Count = softs.Any(s => s.Tissue == t) ? softs.FirstOrDefault(s => s.Tissue == t).Count : null }).ToList(); for (int i = 0; i < Options.Count; i++) { if (Options[i].Count != individual.Tissues[i].Count) { if (individual.Tissues[i].Count == 0) { var soft = await softs .SingleOrDefaultAsync(s => s.IndividualId == individual.Id && s.SoftType == individual.SoftType && s.Tissue == (Tissue)i).ConfigureAwait(false); _context.Soft.Remove(soft); } else { if (softs.Any(s => s.Tissue == (Tissue)i)) { var soft = softs.SingleOrDefault(s => s.Tissue == (Tissue)i); soft.Count = individual.Tissues[i].Count; _context.Soft.Update(soft); } else { var soft = new Soft { IndividualId = individual.Id, SoftType = individual.SoftType, Tissue = (Tissue)i, Count = individual.Tissues[i].Count }; _context.Soft.Add(soft); } } } } } else { if (individual.Configs["degree"]) { var Options = Enum.GetValues(typeof(Tissue)) .Cast <Tissue>() .Select(t => new { Degree = softs.Any(s => s.Tissue == t) ? softs.FirstOrDefault(s => s.Tissue == t).Degree : null }).ToList(); for (int c = 0; c < Options.Count; c++) { if (individual.Tissues[c].Degree == Degree.d0) { if (Options[c].Degree == null) { continue; } else { var soft = await softs .SingleOrDefaultAsync(s => s.IndividualId == individual.Id && s.SoftType == individual.SoftType && s.Tissue == (Tissue)c).ConfigureAwait(false); _context.Soft.Remove(soft); } } else if (Options[c].Degree == null && individual.Tissues[c].Degree != Degree.d0) { var soft = new Soft { IndividualId = individual.Id, SoftType = individual.SoftType, Tissue = (Tissue)c, Degree = individual.Tissues[c].Degree }; _context.Add(soft); } else if (individual.Tissues[c].Degree != Options[c].Degree) { var soft = softs.SingleOrDefault(s => s.Tissue == (Tissue)c); soft.Degree = individual.Tissues[c].Degree; _context.Soft.Update(soft); } } } else { var Options = Enum.GetValues(typeof(Tissue)) .Cast <Tissue>() .Select(t => new { Check = softs.Any(s => s.Tissue == t) }).ToList(); for (int i = 0; i < Options.Count; i++) { if (Options[i].Check != individual.Tissues[i].Check) { if (individual.Tissues[i].Check) { var soft = new Soft { IndividualId = individual.Id, SoftType = individual.SoftType, Tissue = (Tissue)i }; _context.Soft.Add(soft); } else { var soft = await softs .SingleOrDefaultAsync(s => s.IndividualId == individual.Id && s.SoftType == individual.SoftType && s.Tissue == (Tissue)i).ConfigureAwait(false); _context.Soft.Remove(soft); } } } } } } else { var soft = _context.Soft .FirstOrDefault( m => m.IndividualId == individual.Id && m.SoftType == individual.SoftType); if (individual.Configs["count"]) { if (soft == null) { if (individual.Count > 0) { var nuevo = new Soft { IndividualId = individual.Id, SoftType = individual.SoftType, Count = individual.Count }; _context.Soft.Add(nuevo); } } else if (soft.Count != individual.Count) { if (individual.Count == 0) { _context.Soft.Remove(soft); } else { soft.Count = individual.Count; _context.Soft.Update(soft); } } } else { if (soft == null && individual.Check) { var nuevo = new Soft { IndividualId = individual.Id, SoftType = individual.SoftType }; _context.Soft.Add(nuevo); } else if (soft != null && !individual.Check) { _context.Soft.Remove(soft); } } } await _context.SaveChangesAsync().ConfigureAwait(false); return(RedirectToAction("Details", "Samplings", new { Id = individual.SamplingId })); } catch (DbUpdateConcurrencyException) { if (!IndividualExists(individual.Id)) { return(NotFound()); } else { throw; } } } return(View("Error", individual)); }