public ActionResult AddEditDetalleGasto(Int32?DetalleGastoId, Int32 GastoId, Int32 EdificioId) { AddEditDetalleGastoViewModel ViewModel = new AddEditDetalleGastoViewModel(); ViewModel.DetalleGastoId = DetalleGastoId; ViewModel.GastoId = GastoId; ViewModel.EdificioId = EdificioId; ViewModel.Fill(CargarDatosContext()); return(View(ViewModel)); }
public ActionResult AddEditDetalleGasto(AddEditDetalleGastoViewModel ViewModel, FormCollection formCollection) { if (!ModelState.IsValid) { ViewModel.Fill(CargarDatosContext()); TryUpdateModel(ViewModel); return(View(ViewModel)); } try { DetalleGasto _detallegasto = null; if (ViewModel.DetalleGastoId.HasValue) { _detallegasto = context.DetalleGasto.FirstOrDefault(x => x.DetalleGastoId == ViewModel.DetalleGastoId.Value); context.Entry(_detallegasto).State = System.Data.Entity.EntityState.Modified; } else { _detallegasto = new DetalleGasto(); _detallegasto.GastoId = ViewModel.GastoId; _detallegasto.Estado = ConstantHelpers.EstadoActivo; _detallegasto.FechaRegistro = DateTime.Now; context.DetalleGasto.Add(_detallegasto); } _detallegasto.Concepto = ViewModel.Concepto; var montosAdicionales = ViewModel.MontoAdicional.Replace("-", "+-").Split('+'); Decimal SumMontoAdicional = 0; foreach (var ma in montosAdicionales) { if (!String.IsNullOrEmpty(ma)) { SumMontoAdicional += ma.ToDecimal(); } } _detallegasto.Monto = ViewModel.Monto.ToDecimal() + SumMontoAdicional; _detallegasto.Pagado = ViewModel.Pagado; _detallegasto.Ordinario = ViewModel.Ordinario; _detallegasto.Orden = ViewModel.Orden; var camposAdicionales = formCollection.AllKeys.Where(X => X.StartsWith("campo")).Select(X => X.Substring(5)).ToList(); String detalle = ""; foreach (var campo in camposAdicionales) { Decimal d = 0; try { var frmCampo = formCollection["detalle" + campo]; if (!String.IsNullOrEmpty(frmCampo)) { var arraySuma = frmCampo.Split('+'); foreach (var digito in arraySuma) { if (!String.IsNullOrEmpty(digito)) { d += digito.ToDecimal(); } } } //d = formCollection["detalle" + campo].ToDecimal(); } catch (Exception ex) { d = 0; } detalle += campo + ";" + d.ToString("#,##0.00") + "|"; } _detallegasto.Detalle = detalle; context.SaveChanges(); PostMessage(MessageType.Success); } catch { PostMessage(MessageType.Error); } return(RedirectToAction("LstDetalleGastoAdmin", new { GastoId = ViewModel.GastoId, EdificioId = ViewModel.EdificioId })); }