Пример #1
0
        public async Task <IActionResult> Create([Bind("CategoryID,CategoryName")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Пример #2
0
        public async Task <IActionResult> Create([Bind("SongID,NameSong,Singer,Author")] Song song, int[] categoriesId)
        {
            // Lấy danh sách category id khi người dùng check qua mảng categoryId
            if (ModelState.IsValid)
            {
                foreach (var id in categoriesId)
                {
                    var          category     = _context.Category.Find(id);
                    SongCategory songCategory = new SongCategory
                    {
                        Category = category,
                        Song     = song
                    };
                    _context.Add(songCategory);
                }
                _context.Add(song);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(song));
        }