コード例 #1
0
        public async Task <IActionResult> Create([Bind("LevelId,LevelName")] Level level)
        {
            if (ModelState.IsValid)
            {
                _context.Add(level);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(level));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("RecipeId,RecipeTitle,Image1,Image2,Image3,LevelId,DateAdded")] Recipe recipe, string[] StepsTextBox, string[] IngredientsTextbox, IFormFile image1, IFormFile image2, IFormFile image3)
        {
            if (ModelState.IsValid)
            {
                Recipe recipe1 = new Recipe
                {
                    RecipeTitle = recipe.RecipeTitle,
                    DateAdded   = DateTime.Now,
                    LevelId     = recipe.LevelId
                };


                if (image1 != null)
                {
                    var FileName = Guid.NewGuid() + Path.GetExtension(image1.FileName);
                    var SavePath = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "wwwroot", "uploads", FileName);
                    using (var stream = new FileStream(SavePath, FileMode.Create))
                    {
                        image1.CopyTo(stream);
                    }
                    recipe1.Image1 = FileName;
                }

                if (image2 != null)
                {
                    var FileName = Guid.NewGuid() + Path.GetExtension(image2.FileName);
                    var SavePath = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "wwwroot", "uploads", FileName);
                    using (var stream = new FileStream(SavePath, FileMode.Create))
                    {
                        image2.CopyTo(stream);
                    }
                    recipe1.Image2 = FileName;
                }

                if (image3 != null)
                {
                    var FileName = Guid.NewGuid() + Path.GetExtension(image3.FileName);
                    var SavePath = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "wwwroot", "uploads", FileName);
                    using (var stream = new FileStream(SavePath, FileMode.Create))
                    {
                        image3.CopyTo(stream);
                    }
                    recipe1.Image3 = FileName;
                }

                _context.Add(recipe1);
                _context.SaveChanges();

                foreach (string Step in StepsTextBox)
                {
                    if (Step != null)
                    {
                        Steps stp = new Steps
                        {
                            StepName = Step,
                            RecipeId = recipe1.RecipeId
                        };
                        _context.Add(stp);
                    }
                }

                foreach (string Ingredients in IngredientsTextbox)
                {
                    if (Ingredients != null)
                    {
                        Ingredient stp = new Ingredient
                        {
                            IngredientName = Ingredients,
                            RecipeId       = recipe1.RecipeId
                        };
                        _context.Add(stp);
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["LevelId"] = new SelectList(_context.Level, "LevelId", "LevelName", recipe.LevelId);
            return(View(recipe));
        }