public class BlogPost { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime DatePosted { get; set; } }
public class BlogPostController : Controller { private readonly DbContext _context; public BlogPostController(DbContext context) { _context = context; } public IActionResult Index() { var posts = _context.BlogPosts.ToList(); return View(posts); } public IActionResult Create() { return View(); } [HttpPost] public IActionResult Create(BlogPost post) { _context.BlogPosts.Add(post); _context.SaveChanges(); return RedirectToAction("Index"); } }
@model ListIn the above examples, the ASP.NET MVC package library was used to create a simple blog application. The blog post model defines the properties of each post, the controller handles the logic for creating and displaying posts, and the view renders the posts on the page.Blog Posts
@foreach (var post in Model) {}@post.Title
@post.Content
@post.DatePosted