Exemplo n.º 1
0
        public async Task <IActionResult> Create(DishCreatingModel request)
        {
            var dto = Mapper.Map <DishCreateDto>(request);

            // путь к папке Files
            string path = string.Empty;

            if (request.WorkToFile != null)
            {
                byte[] imageData = null;
                // считываем переданный файл в массив байтов
                using (var binaryReader = new BinaryReader(request.WorkToFile.OpenReadStream()))
                {
                    imageData = binaryReader.ReadBytes((int)request.WorkToFile.Length);
                }

                dto.File        = imageData;
                dto.PictureName = request.WorkToFile.FileName;
                path            = _appEnvironment.WebRootPath + "/Images/Dish/";
            }

            var result = await _service.CreateItemAsync(dto, path);

            if (result.IsSuccess)
            {
                return(RedirectToAction("Index", new { organizationId = request.OrganizationId }));
            }
            else
            {
                using (var unitOfWork = _unitOfWorkFactory.MakeUnitOfWork())
                {
                    foreach (var resultError in result.Errors)
                    {
                        ModelState.AddModelError("Error", resultError);
                    }

                    request.OrganizationList = new SelectList(unitOfWork.Organization.GetAll(), "Id", "Name", request.OrganizationId);
                    return(View(request));
                }
            }
        }