Skip to content

Instantly share code, notes, and snippets.

@mikeedwards83
Created February 3, 2013 17:48
Show Gist options
  • Select an option

  • Save mikeedwards83/4702788 to your computer and use it in GitHub Desktop.

Select an option

Save mikeedwards83/4702788 to your computer and use it in GitHub Desktop.
@inherits System.Web.Mvc.WebViewPage<UserGroup.Models.CommentPage>
@using System.Web.Mvc.Html
@using Sitecore.Mvc
@using Sitecore.Mvc.Contrib.Html;
@Html.Sitecore().Placeholder("webedit")
<div>
<h3>Comments</h3>
@if (Model.CommentAdded)
{
<div>
Your comment has been submitted and is awaiting approval
</div>
}
<div>
@Html.ValidationSummary("Comment Errors")
<h4>Add Comment</h4>
@using (Html.BeginForm())
{
<p>
@Html.ValidationMessage("FullName")
@Html.ScLabelFor(x=>x.Comment.Content)
@Html.TextBox("FullName")
</p>
<p>
@Html.ValidationMessage("Email")
@Html.Label("Email", "Email")
@Html.TextBox("Email")
</p>
<p>
@Html.ValidationMessage("Content")
@Html.LabelFor(x=>x.Comment.Content)
@Html.TextArea("Content")
</p>
<input type="submit" value="Add" name="addComment" />
}
</div>
<div>
<h2>Comment Item</h2>
@Html.ScLabelFor(x=>x.CommentItem.Content)
@Html.ScLabelFor(x=>x.CommentItem.Email )
@Html.LabelFor(x=>x.CommentItem.FullName)
</div>
<div>
@if (Model.CommentFolder != null && Model.CommentFolder.Comments.Any())
{
<ul>
@foreach (var comment in Model.CommentFolder.Comments)
{
<li>
From: <span class="Author">@comment.FullName</span>
<p>
@comment.Content
</p>
</li>
}
</ul>
}
else
{
<p>Be the first to comment</p>
}
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sitecore.Mvc.Controllers;
using Glass.Sitecore.Mapper;
using Sitecore.SecurityModel;
using System.Web.UI.WebControls;
using Sitecore.Mvc.Presentation;
using UserGroup.Models;
namespace UserGroup.Controllers
{
public class CommentController : SitecoreController
{
ISitecoreContext _context;
ISitecoreService _master;
/// <summary>
/// This constructor can be used with dependency injection or unit testing
/// </summary>
/// <param name="context"></param>
public CommentController(ISitecoreContext context, ISitecoreService master)
{
_context = context;
_master = master;
}
[HttpGet]
public override ActionResult Index()
{
var model = _context.CreateClass<CommentPage>(false, false, PageContext.Current.Item);
return View(model);
}
[HttpPost]
public ActionResult Index(Comment comment)
{
var webModel = _context.CreateClass<CommentPage>(false, false, PageContext.Current.Item);
if (ModelState.IsValid && comment != null)
{
var masterModel = _master.GetItem<CommentPage>(webModel.Id);
if (masterModel.CommentFolder == null)
{
CommentFolder folder = new CommentFolder();
folder.Name = "Comments";
using (new SecurityDisabler())
{
_context.Create(masterModel, folder);
}
masterModel.CommentFolder = folder;
}
using (new SecurityDisabler())
{
comment.Name = DateTime.Now.ToString("yyyyMMddhhmmss");
//create the comment in the master database
_master.Create(masterModel.CommentFolder, comment);
webModel.CommentAdded = true;
}
}
return View(webModel);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment