コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,PaymentValue,PaymentDate,PaymentComment,SupplierId,CreationDate,CreationBy,IsActive,UserId")] SuppliersPayments suppliersPayments)
        {
            if (ModelState.IsValid)
            {
                suppliersPayments.UserId       = _userManager.GetUserId(User);
                suppliersPayments.CreationDate = DateTime.Now;
                _context.Add(suppliersPayments);
                var supplier = _context.Suppliers.Find(suppliersPayments.SupplierId);
                if (supplier != null)
                {
                    supplier.TotalRemaining = (supplier.TotalRemaining == null ? 0 : supplier.TotalRemaining) - suppliersPayments.PaymentValue;
                    _context.Suppliers.Update(supplier);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SupplierId"] = new SelectList(_context.Suppliers, "Id", "Name", suppliersPayments.SupplierId);
            ViewData["UserId"]     = new SelectList(_context.AspNetUsers, "Id", "Id", suppliersPayments.UserId);
            return(View(suppliersPayments));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,PaymentValue,PaymentDate,PaymentComment,SupplierId,CreationDate,CreationBy,IsActive,UserId")] SuppliersPayments suppliersPayments)
        {
            if (id != suppliersPayments.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    suppliersPayments.UserId = _userManager.GetUserId(User);
                    _context.Update(suppliersPayments);
                    var oldPaid  = _context.SuppliersPayments.Find(suppliersPayments.Id).PaymentValue;
                    var supplier = _context.Suppliers.Find(suppliersPayments.SupplierId);
                    if (supplier != null)
                    {
                        supplier.TotalRemaining = (supplier.TotalRemaining == null ? 0 : supplier.TotalRemaining) - suppliersPayments.PaymentValue + oldPaid;
                        _context.Suppliers.Update(supplier);
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SuppliersPaymentsExists(suppliersPayments.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SupplierId"] = new SelectList(_context.Suppliers, "Id", "Name", suppliersPayments.SupplierId);
            ViewData["UserId"]     = new SelectList(_context.AspNetUsers, "Id", "Id", suppliersPayments.UserId);
            return(View(suppliersPayments));
        }