// GET: Vales/Create public IActionResult Create() { var clientes = from Cliente c in _context.Cliente select new { c.Id, Name = String.Join(" / ", c.NoCliente, c.Name, String.Format(new InterceptProvider(), "{0:U}", c.Id)) }; var honorario = from Honorario h in Enum.GetValues(typeof(Honorario)).Cast <Honorario>().Where(h => h != Honorario.SaldoInicial) select new { Id = h, h.GetType().GetMember(h.ToString()).FirstOrDefault().GetCustomAttribute <DisplayAttribute>(false).Name, h.GetType().GetMember(h.ToString()).FirstOrDefault().GetCustomAttribute <DisplayAttribute>(false).GroupName }; //https://www.google.com/url?q=https://stackoverflow.com/questions/36343620/selectlist-with-selectlistgroup&sa=D&ust=1517165157558000&usg=AFQjCNEno9tPOiWA4l7nPN0afQZRgOwagg var initialModel = new ValeViewModel { ClientesList = new SelectList(clientes, "Id", "Name"), HonorariosList = new List <SelectListItem> { } }; foreach (var group in honorario.GroupBy(h => h.GroupName)) { var optionGroup = new SelectListGroup() { Name = group.Key }; foreach (var hon in group) { initialModel.HonorariosList.Add(new SelectListItem() { Value = hon.Id.ToString(), Text = hon.Name, Group = optionGroup }); } } return(View(initialModel)); }
public async Task <IActionResult> ValeCliente(int?id) { if (id == null) { return(NotFound()); } var clientes = from Cliente c in _context.Cliente select new { c.Id, Name = String.Join(" / ", c.NoCliente, c.Name, String.Format(new InterceptProvider(), "{0:U}", c.Id)) }; var honorario = from Honorario h in Enum.GetValues(typeof(Honorario)).Cast <Honorario>().Where(h => h != Honorario.SaldoInicial) select new { Id = h, h.GetType().GetMember(h.ToString()).FirstOrDefault().GetCustomAttribute <DisplayAttribute>(false).Name, h.GetType().GetMember(h.ToString()).FirstOrDefault().GetCustomAttribute <DisplayAttribute>(false).GroupName }; var cliente = await _context.Cliente.FirstOrDefaultAsync(c => c.Id == id); var model = new ValeViewModel { ClienteId = id.Value, ClientesList = new SelectList(clientes, "Id", "Name"), //SaldosList = new SelectList(_context.Cliente, "Id", "SaldosPendientes"), //HonorariosList = new SelectList(honorario, "Id", "Name"), HonorariosId = new List <int> { (int)Honorario.Mensuales, (int)Honorario.Laborales, (int)Honorario.Renta, (int)Honorario.RetenciĆ³n } }; return(View("Create", model)); }
public async Task <IActionResult> Create([Bind("ClienteId,Cobros,SaldosPendientes,SubTotal,Total")] ValeViewModel vale) { if (ModelState.IsValid) { var model = new Vale { ClienteId = vale.ClienteId, Date = DateTime.Now, SubTotal = Convert.ToInt32(Regex.Replace(vale.SubTotal.ToString(), "[^0-9]", "")), ApplicationUserId = _userManager.GetUserId(User), Cobros = new List <Cobro> { } }; foreach (var cobro in vale.Cobros) { if (String.IsNullOrEmpty(cobro.Monto)) { continue; } var monto = Convert.ToInt32(Regex.Replace(cobro.Monto, "[^0-9]", "")); //var hon = (Honorario)Enum.Parse(typeof(Honorario), cobro.Honorario); //model.Cobros.Add(new Cobro { Monto = monto, Honorario = hon }); model.Cobros.Add( new Cobro { Monto = monto, Honorario = cobro.Honorario }); } var cliente = await _context.Cliente .Include(c => c.Vales) .SingleAsync(c => c.Id == model.ClienteId); cliente.SaldosPendientes += model.SubTotal; var last = cliente.Vales.SingleOrDefault(v => v.State == State.Activo); if (last != null) { last.State = State.Pendiente; _context.Vale.Update(last); } model.State = State.Activo; _context.Vale.Add(model); _context.Cliente.Update(cliente); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } var clientes = from Cliente c in _context.Cliente select new { c.Id, Name = String.Join(" / ", c.NoCliente, c.Name, String.Format(new InterceptProvider(), "{0:U}", c.Id)) }; var honorario = from Honorario h in Enum.GetValues(typeof(Honorario)).Cast <Honorario>().Where(h => h != Honorario.SaldoInicial) select new { Id = h, h.GetType().GetMember(h.ToString()).FirstOrDefault().GetCustomAttribute <DisplayAttribute>(false).Name, h.GetType().GetMember(h.ToString()).FirstOrDefault().GetCustomAttribute <DisplayAttribute>(false).GroupName }; var initialModel = new ValeViewModel { ClientesList = new SelectList(clientes, "Id", "Name"), HonorariosList = new List <SelectListItem> { } }; foreach (var group in honorario.GroupBy(h => h.GroupName)) { var optionGroup = new SelectListGroup() { Name = group.Key }; foreach (var hon in group) { initialModel.HonorariosList.Add(new SelectListItem() { Value = hon.Id.ToString(), Text = hon.Name, Group = optionGroup }); } } return(View(initialModel)); }