コード例 #1
0
        public async Task <IActionResult> AddImage([Bind("Name, ImageFile, RecipeId")] RecipeImagesModel imageModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(imageModel));
            }

            string wwwRootPath = this.webHostEnvironment.WebRootPath;
            string fileName    = Path.GetFileNameWithoutExtension(imageModel.ImageFile.FileName);
            string extension   = Path.GetExtension(imageModel.ImageFile.FileName);

            imageModel.Name = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
            string path = Path.Combine(wwwRootPath + "/img/", fileName);

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                await imageModel.ImageFile.CopyToAsync(fileStream);
            }

            var image = this.mapper.Map <Image>(imageModel);

            this.context.Add(image);
            await this.context.SaveChangesAsync();

            return(this.RedirectToAction("Details", "Recipes", new { id = imageModel.RecipeId }));
        }
コード例 #2
0
        public IActionResult AddImage(int recipeId)
        {
            if (!this.recipes.Exist(recipeId))
            {
                return(this.NotFound("There is no recipe with given id"));
            }

            var imageModel = new RecipeImagesModel();

            imageModel.RecipeId = recipeId;

            return(this.View(imageModel));
        }