Skip to content

Instantly share code, notes, and snippets.

@Lunchbox4K
Last active August 29, 2015 14:27
Show Gist options
  • Save Lunchbox4K/6664ea71e45c7ea2b417 to your computer and use it in GitHub Desktop.
Save Lunchbox4K/6664ea71e45c7ea2b417 to your computer and use it in GitHub Desktop.
ASP MVC Blog
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MitchellsWebBlog.Models
{
public class BlogPost
{
public int ID { get; set; }
public string user { get; set; }
public string title { get; set; }
[AllowHtml]
public string message { get; set; }
public DateTime timeStamp { get; set; }
}
public class BlogPostDBContext : DbContext
{
public DbSet<BlogPost> BlogPosts { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MitchellsWebBlog.Models;
namespace WebBlog.Controllers
{
public class BlogPostsController : Controller
{
private BlogPostDBContext db = new BlogPostDBContext();
// GET: /BlogPosts/
public ActionResult Index()
{
return View(db.BlogPosts.ToList());
}
// GET: /BlogPosts/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
BlogPost blogpost = db.BlogPosts.Find(id);
if (blogpost == null)
{
return HttpNotFound();
}
return View(blogpost);
}
// GET: /BlogPosts/Create
public ActionResult Create()
{
return View();
}
// POST: /BlogPosts/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="ID,title,message")] BlogPost blogpost)
{
blogpost.user = User.Identity.Name;
blogpost.timeStamp = DateTime.Now;
if (ModelState.IsValid)
{
db.BlogPosts.Add(blogpost);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(blogpost);
}
// GET: /BlogPosts/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
BlogPost blogpost = db.BlogPosts.Find(id);
if (blogpost == null)
{
return HttpNotFound();
}
return View(blogpost);
}
// POST: /BlogPosts/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,title,message")] BlogPost blogpost)
{
blogpost.timeStamp = DateTime.Now;
if (ModelState.IsValid)
{
db.Entry(blogpost).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(blogpost);
}
// GET: /BlogPosts/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
BlogPost blogpost = db.BlogPosts.Find(id);
if (blogpost == null)
{
return HttpNotFound();
}
return View(blogpost);
}
// POST: /BlogPosts/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
BlogPost blogpost = db.BlogPosts.Find(id);
db.BlogPosts.Remove(blogpost);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
@model MitchellsWebBlog.Models.BlogPost
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>BlogPost</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.title, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.message, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.message)
@Html.ValidationMessageFor(model => model.message)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back", "Index", "Home", null, null)
@Html.Raw(" | ")
@Html.ActionLink("Blogpost List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model MitchellsWebBlog.Models.BlogPost
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>BlogPost</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.title, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.message, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.message)
@Html.ValidationMessageFor(model => model.message)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back", "Index", "Home", null, null)
@Html.Raw(" | ")
@Html.ActionLink("Blogpost List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MitchellsWebBlog.Models;
namespace WebBlog.Controllers
{
public class HomeController : Controller
{
private BlogPostDBContext db = new BlogPostDBContext();
public ActionResult Index()
{
return View(db.BlogPosts.ToList());
}
}
}
@model IEnumerable<MitchellsWebBlog.Models.BlogPost>
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>Mitchell's ASP Blog</h1>
</div>
<div class="row">
<div>
<!-- Used to change background of posts-->
@{bool everyOther = false;}
@foreach (var item in Model)
{
<div @if(everyOther){ @Html.Raw("style='background-color: #F0FFFF'") }
@if(!everyOther){ @Html.Raw("style='background-color: #FFFFCC'")}>
<h1>@Html.DisplayFor(modelItem => item.title)</h1>
<p>@Html.Raw(item.message)</p>
@{everyOther = !everyOther;}
<hr />
<h6>@Html.DisplayFor(modelItem => item.user) | @Html.DisplayFor(modelItem => item.timeStamp)
@if (item.user == User.Identity.Name)
{
@Html.ActionLink("Edit", "Edit", "BlogPosts", new { id = item.ID }, new { id = item.ID })
@Html.Raw("|")
@Html.ActionLink("Details", "Details", "BlogPosts", new { id = item.ID }, new { id = item.ID })
@Html.Raw("|")
@Html.ActionLink("Delete", "Delete", "BlogPosts", new { id = item.ID }, new { id = item.ID })
}</h6>
<hr />
</div>
}
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment