Simple CMS with Linq to SQL part II

In our last installment, we looked at the basics of creating and deleting pages and comments. Now we’re going to dive into the views and editing portion including listing pages and comments in index (list) form and paging.

First, we can compile our project to make sure there no compile errors. If all went well, and if you used the mod portal modifications, you will be greeted with the following :

Welcome to your project

We’ll worry about the front page later. First let’s go into our PagesController and add a few views. I don’t want limit our flexibility with multiple full page views, so we’ll only have them for the Index, Read, Edit, Delete, EditComment and finally DeleteComment views. The rest will be strongly typed partial views (ViewUserControls).

Remember that the last time we didn’t include any programming to accomplish editing, but don’t worry. It’s fairly simple.

First, let’s create a control to display an index list of paged… er… content pages. You can create an empty control first called PageList, but we’ll need to modify the Inherits flag to match our PagedList class so we can access that model.

<%@ Control Language="C#" 
Inherits="System.Web.Mvc.ViewUserControl<PagedList<SimpleCMS.Models.ContentPage>>" %>
    <% foreach (var item in Model) { %>
    <div class="post">
           <h4><%: Html.ActionLink(item.Title, "Read", new { id = item.PageId }, 
                	new { title = Html.AltTitle(item.Title, item.Description) }) %></h4>

			<p class="data"><span><%: Html.FormalDate(item.PubDate) %> | 
			by <%: Html.AuthorLink(item.Author, item.AuthorId) %>.</span> 
			<span><%: Html.EditedDate(item.PubDate, item.LastModified) %>
			<%: Html.ActionLink("Edit", "Edit", new { id=item.PageId }) %> | 
			<%: Html.ActionLink("Delete", "Delete", new { id=item.PageId })%> | 
			<%: Html.CommentsLink(item.PageId, item.CommentCount) %> | 
			Viewed <%: item.ViewCount %> times</span></p>

                <% if (!String.IsNullOrEmpty(item.Abstract)) { %>
				<%= item.Abstract %>
				<%  } else { %>
				<%= item.BodyHtml %><% } %>			
    </div>
    <% } %>

Notice that there are multiple inheritences/implementations here. Our PagedList implements ContentPage, since that’s what we’re displaying in this control. Also note that we’re making use of a couple of helper functions, EditedDate and CommentsLink in the MvcHelpers class we created the last time.

Now we can view our pages, let’s also make another control that allows us to create them… Note, this has to be a strongly typed control called CreatePage that uses the ContentPage model.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SimpleCMS.Models.ContentPage>" %>
<div class="postform">
	<%
		using (Html.BeginForm("Create", "Pages", new { id = ViewData.Model.PageId }, FormMethod.Post))
		{%>
	<%: Html.ValidationSummary(true)%>
	<fieldset>
		<legend>Create new page</legend>
		<p>
			<label for="Title">Title</label>
			<%: Html.TextBoxFor(model => model.Title)%><%: Html.ValidationMessageFor(model => model.Title, "*")%></p>
		<p>
			<label for="Description">Description</label>
			<%: Html.TextBoxFor(model => model.Description)%>
		</p>
		<p>
			<label for="Abstract">Abstract</label>
			<%: Html.TextAreaFor(model => model.Abstract, new { rows = 5, cols = 60 })%>
		</p>
		<p>
			<label for="Author">Author</label>
			<%: Html.TextBoxFor(model => model.Author)%>
		</p>
		<p>
			<label for="BodyText">Content</label>
			<%: Html.TextAreaFor(model => model.BodyText, new { rows = 5, cols = 60 })%>
			<%: Html.ValidationMessageFor(model => model.BodyText)%>
		</p>
		<p>
			<label for="PubDate">Publish Date</label>
			<%: Html.TextBoxFor(model => model.PubDate)%>
		</p>
		<ul class="feedback options">
			<li><label><input type="checkbox" value="true" name="EnableComments" 
			<%= Html.CheckBoxChecked(ViewData.Model.EnableComments, true) %>/> Enable Comments</label></li>
			<li><label><input type="checkbox" value="true" name="AnonComments" 
			<%= Html.CheckBoxChecked(ViewData.Model.AnonComments, true) %>/> Anonymous Comments</label></li>
			<li><label><input type="checkbox" value="true" name="ModeratedComments" 
			<%= Html.CheckBoxChecked(ViewData.Model.Moderated, false) %>/> Moderated</label></li>
			<li><label><input type="checkbox" value="true" name="Approved" 
			<%= Html.CheckBoxChecked(ViewData.Model.Approved, true) %>/> Approved</label></li>
		</ul>
		<p>
			<input type="submit" value="Create new page" />
		</p>
	</fieldset>
</div>
<% } %>

Now we’re finally ready to create the Index View. Remember, this is going to be a full page strongly typed view, not just an .ascx. We just have to make sure that the strongly typed model is going to be PageView. You can leave View Content option as Empty since we’re going to be writing in our own functionality.

<%@ 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">
Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Index</h2>
	<% 		
		if (Model.Pages.Count > 0)
		{
			// We pages in this PageView, so we can start listing them.
			Html.RenderPartial("PageList", ViewData.Model.Pages);
		}
		if (Model.HasPage)
		{
			// We're adding a new page to an existing one.
			Html.RenderPartial("CreatePage", ViewData.Model.Page);
		}
		else
		{
			// We're creating a new content page from scratch
			Html.RenderPartial("CreatePage", new SimpleCMS.Models.ContentPage());
		}
		%>
</asp:Content>

See how uncluttered our page views are when we delegate all the “stuff” to user controls. Also note that we’re not fiddling with ViewData["somestuff"] and such.

Go ahead and try it out.. You should be able to navigate to the /Pages/ section of your project after a compilation and create new pages.

Onward to the next step…

About these ads

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 )

Google+ photo

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

Connecting to %s