Simple CMS with Linq to SQL part II

After creating our pages, we have to have a way to Delete as well as read and add/read comments on them (we’ll get to editing a bit later).

Add a new Delete full page view and select ContentPage as the data model. I like to display a short paragraph about the page about to be deleted rather than raw field text, which is frankly boring.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<SimpleCMS.Models.ContentPage>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Delete
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Deleting : <%: Model.Title %></h2>
	<p><%: Model.Description %></p>

    <h3>Warning: You're about to delete a page! This process cannot be undone. 
	Are you sure, you wish to continue?</h3>
	<p>
		<%: Model.Title %> was viewed <%: Model.ViewCount %> times and has 
		<%: Model.PageCount %> sub pages as well as <%: Model.CommentCount %> comments. 
		The page was created <%: Html.FriendlyDate(Model.CreatedDate) %> and 
		last modified <%:Html.FriendlyDate(Model.LastModified) %>
	</p>

	<% using (Html.BeginForm()) { %>
		<p><input type="submit" value="Delete this page" /> | 
			<%: Html.ActionLink("Back to List", "Index") %>
		</p>
	<% } %>

	<%= Model.BodyHtml %>

</asp:Content>

We now have to have feedback capability via a comment form on our upcoming Read view.

Let’s add that comment form now, called CreateComment as a strongly typed control using the ContentComment model.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SimpleCMS.Models.ContentComment>" %>
<% using (Html.BeginForm("CreateComment", "Pages", new { id = ViewData.Model.PageId }, FormMethod.Post))
   {%>
<%: Html.ValidationSummary(true) %>
<div class="postform">
	<fieldset>
		<legend>New Comment</legend>
		<%: Html.HiddenFor(model => model.PageId) %>
		<p>
			<%: Html.LabelFor(model => model.Author) %>
			<%: Html.TextBoxFor(model => model.Author) %>
			<%
				// We don't have anonymous comments enabled, the "Author" field is required
				if (!(bool)Model.ContentPage.AnonComments)
					Html.ValidationMessageFor(model => model.Author); %>
		</p>
		<p>
			<%: Html.LabelFor(model => model.AuthorEmail) %>
			<%: Html.TextBoxFor(model => model.AuthorEmail) %>
			<%if (!(bool)Model.ContentPage.AnonComments)
		 Html.ValidationMessageFor(model => model.AuthorEmail); %>
		</p>
		<p>
			<%: Html.LabelFor(model => model.BodyText) %>
			<%: Html.TextAreaFor(model => model.BodyText, new { rows = 5, cols = 60 })%>
			<%: Html.ValidationMessageFor(model => model.BodyText) %>
		</p>
		<p>
			<input type="submit" value="Post new Comment" />
		</p>
	</fieldset>
</div>
<% } %>

After creating these comments, we then need to be able to display them. But remember that we’re showing a paged list of comments, so we need to modify the Inherits flag as we did for the PageList control.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PagedList<SimpleCMS.Models.ContentComment>>" %>
<ul class="posts" id="comments">
	<% foreach (var item in Model)
	{ %>
	<li>
		<h5>Posted <%: Html.FriendlyDate(item.CreatedDate) %> | By <%: item.Author %></h5>
		<%= item.BodyHtml %>
		<p class="functions">
			<span><%: Html.ActionLink("Edit", "EditComment", new { id=item.CommentId }) %> | 
				<%: Html.ActionLink("Delete", "DeleteComment", new { id=item.CommentId })%></span>
		</p>
	</li>
	<% } %>
</ul>

Now we’re ready to add these controls to our read page view. Create a new view called Read (not a partial view, remember) and select the PageView model as the view data class..

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<SimpleCMS.Models.PageView>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	<%: Model.Page.Title %> - SimpleCMS
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
	<h2><%: Model.Page.Title %></h2>

	<%= Model.Page.BodyHtml %>

	<%= Html.Pager(Model.Comments as IPagedList, "/Pages/Read/" + Model.Page.PageId +"/###") %>
	<%
		// If comments are enabled, render the comment form.
		if ((bool)Model.Page.EnableComments)
		{
			var comment = new SimpleCMS.Models.ContentComment();
			comment.ContentPage = Model.Page;

			Html.RenderPartial("CommentList", Model.Comments);
			Html.RenderPartial("CreateComment", comment);
		} %>
</asp:Content>

Note how we’re again using the one of those MvcHelpers class functions from the last installment. In this case Pager needs to have raw HTML output, hence the equal (=) sign instead of the colon (:). The same applies to the BodyHtml of the model.

Now after compiling, you should be able to create read and add comments to your pages.

Onward to step 3…

3 thoughts on “Simple CMS with Linq to SQL part II

  1. Pingback: Simple CMS with Linq to SQL part II-a « This page intentionally left ugly

  2. I don’t know what this is all about, but it could sure use a more tag right near the top of the first page! (you don’t have to publish this)

    • thinsmek, I don’t censor comments that aren’t outright spam or have hate speech ;)

      I used the next best thing to the more tag… the nextpage tag.

      The more tag had caused some issues with my blog in the past, which is why I rarely use it. A lot of my content is raw code, so that may have something to do with it. This is an introduction to writing a content management system from scratch.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s