Beispiel #1
0
        public async Task <IActionResult> Create([Bind("GroupId,Name")] Group @group)
        {
            if (ModelState.IsValid)
            {
                _context.Add(@group);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(@group));
        }
Beispiel #2
0
        public async Task <IActionResult> Add(NewsAddViewModel newsVM)
        {
            if (!ModelState.IsValid)
            {
                return(View(newsVM));
            }

            var news = new News()
            {
                CreateDate  = DateTime.Now,
                Description = newsVM.Description,
                GroupId     = newsVM.Group,
                InSlider    = newsVM.InSlider,
                Tages       = newsVM.Tages,
                Text        = newsVM.Text,
                Title       = newsVM.Title,
                Views       = 0,
                UserId      = 1
            };

            await _context.AddAsync(news);

            foreach (var item in newsVM.Media)
            {
                if (item.Length > 0)
                {
                    string getExtensionMedia = Path.GetExtension(item.FileName);

                    string newNameMedia = Guid.NewGuid().ToString();

                    string filePath = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "wwwroot",
                        "ImagesNews",
                        newNameMedia +
                        getExtensionMedia);


                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        item.CopyTo(stream);
                    }

                    var media = new Media()
                    {
                        MediaFileType = MediaFileType.Img,
                        Name          = newNameMedia + getExtensionMedia,
                        News          = news,
                    };


                    news.Medias.Add(media);

                    _context.Add(media);
                }
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }