Notes
![]() ![]() Notes - notes.io |
using System.Collections.Generic;
using System.Formats.Asn1;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using CSharp.MVCAdminPanel.Data;
using CSharp.MVCAdminPanel.Models;
namespace CSharp.MVCAdminPanel.Areas.Admin.Controllers
{
[Area("Admin")]
public class PostsController : Controller
{
private readonly ApplicationDbContext _context;
public PostsController(ApplicationDbContext context)
{
_context = context;
}
// GET: Admin/Posts
public async Task<IActionResult> Index()
{
var AdminPanelContext = _context.Posts
.Include(p => p.Category)
.Include(p => p.PostPopularTags)
.ThenInclude(p => p.PopularTag)
.AsNoTracking()
.ToListAsync();
return View(await AdminPanelContext);
}
// GET: Admin/Posts/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var post = await _context.Posts
.Include(p => p.Category)
.FirstOrDefaultAsync(m => m.Id == id);
if (post == null)
{
return NotFound();
}
return View(post);
}
// GET: Admin/Posts/Create
public IActionResult Create()
{
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title");
ViewData["PostPopularTags"] = new SelectList(_context.PopularTags, "Id", "Title");
return View();
}
// POST: Admin/Posts/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Title,Content,ImageUrl,PublishDate,CategoryId,Category,Id")] Post post, int[] PostPopularTags)
{
if (ModelState.IsValid)
{
if (PostPopularTags.Length > 0)
{
var tags = PostPopularTags.Select(t => new PopularTagPost() { PopularTagId = t }).ToList();
post.PostPopularTags = tags;
}
_context.Add(post);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title", post.CategoryId);
ViewData["PostPopularTags"] = new SelectList(_context.PopularTags, "Id", "Title");
return View(post);
}
// GET: Admin/Posts/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var post = await _context.Posts
.Include(p => p.Category)
.Include(p => p.PostPopularTags)
.ThenInclude(pt => pt.PopularTag).FirstOrDefaultAsync(p => p.Id == id);
if (post == null)
{
return NotFound();
}
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title", post.CategoryId);
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title", post.PostPopularTags);
return View(post);
}
// POST: Admin/Posts/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Title,Content,ImageUrl,PublishDate,CategoryId,Id")] Post post)
{
if (id != post.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(post);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PostExists(post.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title", post.CategoryId);
return View(post);
}
// GET: Admin/Posts/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var post = await _context.Posts
.Include(p => p.Category)
.FirstOrDefaultAsync(m => m.Id == id);
if (post == null)
{
return NotFound();
}
return View(post);
}
// POST: Admin/Posts/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var post = await _context.Posts.FindAsync(id);
if (post != null)
{
_context.Posts.Remove(post);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool PostExists(int id)
{
return _context.Posts.Any(e => e.Id == id);
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team