示例#1
0
        // GET: Contents/Create

        public IActionResult Create()
        {
            ContentCreateViewModel content = new ContentCreateViewModel();

            ViewData["CategoryID"] = new SelectList(_context.Category, "ID", "CategoryName");
            return(View(content));
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("ID,ContentTitle,ShortDescription,Url,LongDescription,NoOfLikes,IsEdited,IsHeadline,DatePublished,DateEdited,VideoUrl,CategoryID")] ContentCreateViewModel contentView)
        {
            Content content = new Content();

            if (ModelState.IsValid)
            {
                IFormFile file         = contentView.Url;
                string    relativePath = "";
                if (file != null)
                {
                    var fileName = $"{contentView.ID}{Path.GetFileName(file.FileName)}";
                    relativePath = Path.Combine("images", fileName);
                    var absolutePath = Path.Combine(_env.WebRootPath, relativePath);

                    using (FileStream stream = new FileStream(absolutePath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                }

                content = new Content
                {
                    ID               = contentView.ID,
                    ContentTitle     = contentView.ContentTitle,
                    ShortDescription = contentView.ShortDescription,
                    ImageUrl         = relativePath,
                    LongDescription  = contentView.LongDescription,
                    NoOfLikes        = contentView.NoOfLikes,
                    IsEdited         = contentView.IsEdited,
                    IsHeadline       = contentView.IsHeadline,
                    DatePublished    = DateTime.Now,
                    DateEdited       = DateTime.Now,
                    VideoUrl         = contentView.VideoUrl,
                    CategoryID       = contentView.CategoryID
                };


                _context.Add(content);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryID"] = new SelectList(_context.Category, "ID", "CategoryName", content.CategoryID);
            return(View(content));
        }
示例#3
0
        // GET: Contents/Edit/5

        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var content = await _context.Content.FindAsync(id);

            if (content == null)
            {
                return(NotFound());
            }
            ContentCreateViewModel realContent = new ContentCreateViewModel {
                Category  = content.Category, CategoryID = content.CategoryID, ContentTitle = content.ContentTitle, DateEdited = content.DateEdited, DatePublished = content.DatePublished,
                ID        = content.ID, ImageUrl = content.ImageUrl, IsEdited = content.IsEdited, IsHeadline = content.IsHeadline, LongDescription = content.LongDescription,
                NoOfLikes = content.NoOfLikes, ShortDescription = content.ShortDescription, VideoUrl = content.VideoUrl
            };

            ViewData["CategoryID"] = new SelectList(_context.Category, "ID", "CategoryName", content.CategoryID);
            return(View(realContent));
        }
示例#4
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,ContentTitle,ShortDescription,Url,LongDescription,NoOfLikes,IsEdited,IsHeadline,DatePublished,DateEdited,CategoryID,VideoUrl,ImageUrl")] ContentCreateViewModel contentView)
        {
            Content content = new Content();

            if (id != contentView.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                IFormFile file         = contentView.Url;
                string    relativePath = "";
                if (file != null)
                {
                    if (contentView.ImageUrl != null)
                    {
                        var oldPath = Path.Combine(_env.WebRootPath, contentView.ImageUrl);
                        if (System.IO.File.Exists(oldPath))
                        {
                            System.IO.File.Delete(oldPath);
                        }
                    }

                    var fileName = $"{contentView.ID}{Path.GetFileName(file.FileName)}";
                    relativePath = Path.Combine("images", fileName);
                    var absolutePath = Path.Combine(_env.WebRootPath, relativePath);

                    using (FileStream stream = new FileStream(absolutePath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                }
                content = new Content
                {
                    ID               = contentView.ID,
                    ContentTitle     = contentView.ContentTitle,
                    ShortDescription = contentView.ShortDescription,
                    ImageUrl         = (!string.IsNullOrEmpty(relativePath)) ? relativePath : contentView.ImageUrl,
                    LongDescription  = contentView.LongDescription,
                    NoOfLikes        = contentView.NoOfLikes,
                    IsEdited         = contentView.IsEdited,
                    IsHeadline       = contentView.IsHeadline,
                    DatePublished    = contentView.DatePublished,
                    DateEdited       = DateTime.Now,
                    VideoUrl         = contentView.VideoUrl,
                    CategoryID       = contentView.CategoryID
                };
                try
                {
                    _context.Update(content);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ContentExists(content.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryID"] = new SelectList(_context.Category, "ID", "ID", content.CategoryID);
            return(View(contentView));
        }