public async Task <IActionResult> Create([Bind("UserID,PropertyID,PropertyTaxContractImg,ImgPath")] PropertyTaxTransaction propertyTax) { if (ModelState.IsValid) { propertyTax.UserID = (int)HttpContext.Session.GetInt32("UserID"); string ImgPathAfterSave = Image.Save(propertyTax.UserID, propertyTax.PropertyTaxContractImg, propertyTax.PropertyID, "PropertyTax"); if (ImgPathAfterSave == null) // Another file with same name is already exist - "UserID-WaterMeterID" has to be unique { ModelState.AddModelError("", "Another transaction with the same User ID Water Meter ID is already exist."); return(View()); } propertyTax.Status = Config.TransactionStatus.Open; // Init new transaction status propertyTax.ImgPath = ImgPathAfterSave; _context.Add(propertyTax); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["UserID"] = new SelectList(_context.User, "UserID", "UserID", propertyTax.UserID); return(View(propertyTax)); }
public async Task <IActionResult> Edit(int id, [Bind("UserID,PropertyID,PropertyTaxContractImg,ImgPath,Status")] PropertyTaxTransaction propertyTaxTransactionAfterEdit) { var propertyTaxTransactionBeforeEdit = await _context.PropertyTaxTransactions.FindAsync(id); if (propertyTaxTransactionBeforeEdit == null) { return(NotFound()); } if (ModelState.IsValid) { try { if (propertyTaxTransactionAfterEdit.PropertyTaxContractImg != null) { string newImgRelativePath = Image.Edit(propertyTaxTransactionBeforeEdit.UserID, propertyTaxTransactionBeforeEdit.ImgPath, propertyTaxTransactionAfterEdit.PropertyTaxContractImg, propertyTaxTransactionBeforeEdit.PropertyID, "PropertyTax"); propertyTaxTransactionBeforeEdit.ImgPath = newImgRelativePath; } propertyTaxTransactionBeforeEdit.Status = propertyTaxTransactionAfterEdit.Status; _context.Update(propertyTaxTransactionBeforeEdit); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PropertyTaxExists(propertyTaxTransactionAfterEdit.PropertyID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["UserID"] = new SelectList(_context.User, "UserID", "UserID", propertyTaxTransactionAfterEdit.UserID); return(View(propertyTaxTransactionAfterEdit)); }