public async Task <IActionResult> EditModal(int id, [Bind("Id,Name,Price,DishesQuantity,CategoriesId,DishKindId")] Complex cmp, List <DishComplex> DishComplexes) { if (id != cmp.Id) { return(NotFound()); } var complex_orig = await _context.Complex.Include(c => c.DishComplex).ThenInclude(d => d.Dish).AsNoTracking().SingleOrDefaultAsync(c => c.Id == id); DishComplexes.ForEach(dc => dc.Dish = _context.Dish.SingleOrDefaultAsync(c => c.Id == dc.DishId).Result); ViewData["CategoriesId"] = new SelectList(_context.Categories.WhereCompany(User.GetHotelID()).ToList(), "Id", "Name", cmp.CategoriesId); // ViewData["DishKindId"] = new SelectList(GetDishesKindWithEmptyList(), "Value", "Text", cmp.DishKindId); ; if (!ModelState.IsValid) { cmp.DishesQuantity = complex_orig.DishesQuantity; cmp.DishComplex = complex_orig.DishComplex; return(PartialView(cmp)); } if (complex_orig != null) { var validate_error = await _complexRepo.ValidateComplexUpdate(cmp, User.GetHotelID(), DishComplexes, complex_orig.DishComplex.ToList()); if (!validate_error.Success) { ModelState.AddModelError("", validate_error.Error); cmp.DishesQuantity = complex_orig.DishesQuantity; cmp.DishComplex = complex_orig.DishComplex; return(PartialView(cmp)); } } var res = await this.UpdateDBCompanyDataAsyncEx2(cmp, _logger, e => { return(_complexRepo.UpdateComplexEntity(e, DishComplexes, User.GetHotelID())); }); //var res = await this.UpdateDBCompanyDataAsyncEx(cmp, _logger); if (!ModelState.IsValid) { cmp.DishComplex = complex_orig.DishComplex; return(PartialView(cmp)); } return(res); }
public async Task <IActionResult> EditModal([FromForm] int id, [FromForm] Complex cmp, [FromForm] List <DishComplex> DishComplexes) { if (id != cmp.Id) { return(NotFound()); } var complex_orig = await _context.Complex.Include(c => c.DishComplex).ThenInclude(d => d.Dish).AsNoTracking().SingleOrDefaultAsync(c => c.Id == id); DishComplexes.ForEach(dc => dc.Dish = _context.Dishes.SingleOrDefaultAsync(c => c.Id == dc.DishId).Result); ViewData["CategoriesId"] = new SelectList(_context.Categories.WhereCompany(User.GetCompanyID()).ToList(), "Id", "Name", cmp.CategoriesId); ViewData["DishKindId"] = new SelectList(GetDishesKindWithEmptyList(), "Value", "Text", cmp.DishKindId);; if (!ModelState.IsValid) { cmp.DishComplex = complex_orig.DishComplex; return(PartialView(cmp)); } if (complex_orig != null) { var validate_error = await _complexRepo.ValidateComplexUpdate(cmp, User.GetCompanyID(), DishComplexes, complex_orig.DishComplex.ToList()); if (!validate_error.Success) { ModelState.AddModelError("", validate_error.Error); cmp.DishComplex = complex_orig.DishComplex; return(PartialView(cmp)); } } if (Request.Form.Files.Count > 0) { Pictures pict = _context.Pictures.SingleOrDefault(p => p.Id == cmp.PictureId); if (pict == null || true) //to do always new { pict = new Pictures(); } var file = Request.Form.Files[0]; using (var stream = Request.Form.Files[0].OpenReadStream()) { byte[] imgdata = new byte[stream.Length]; stream.Read(imgdata, 0, (int)stream.Length); pict.PictureData = imgdata; PicturesController.CompressPicture(pict, 350, 350); //pict.PictureData = imgdata; } _context.Add(pict); await _context.SaveChangesAsync(); cmp.PictureId = pict.Id; } var res = await this.UpdateDBCompanyDataAsyncEx2(cmp, _logger, e => { return(_complexRepo.UpdateComplexEntity(e, DishComplexes, User.GetCompanyID())); }); //var res = await this.UpdateDBCompanyDataAsyncEx(cmp, _logger); if (!ModelState.IsValid) { cmp.DishComplex = complex_orig.DishComplex; return(PartialView(cmp)); } return(res); }