Esempio n. 1
0
        public async Task <IActionResult> Create([Bind("BlogId,Title,Abstract,Content,IsPublished")] Post post, IFormFile image) //delete the Id in a create when it is scaffolded like this
        {
            if (ModelState.IsValid)
            {
                post.Created = DateTime.Now;
                post.Updated = DateTime.Now;
                post.Slug    = Regex.Replace(post.Title.ToLower(), @"\s", "-");
                //Write Image
                if (image != null)
                {
                    post.FileName = image.FileName;
                    post.Image    = ImageUtility.EncodeImage(image);
                }

                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BlogId"] = new SelectList(_context.Blogs, "Id", "Name", post.BlogId);
            return(View(post));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(int id, [Bind("BlogId,Title,Abstract,Content,IsPublished")] Post post, IFormFile image) //add IformFile image
        {
            if (id != post.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    post.Updated = DateTime.Now;

                    if (image != null)                                   //add line
                    {
                        post.FileName = image.FileName;                  //add line
                        post.Image    = ImageUtility.EncodeImage(image); //add line
                    }

                    _context.Update(post);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostExists(post.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BlogId"] = new SelectList(_context.Blogs, "Id", "Id", post.BlogId);
            return(View(post));
        }