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 })); }
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)); }