Here is the second post of my series of post to help you with the first steps in the EPiServer MVC world.
You can find the other post of this series here:
Of course this is very similar to the creation of a page
In place of inheriting from PageType you should inherit from BlockData now. And I have of course always a base class like this I am able to add some common code to all my custom block data. My base class:
As in the page type, we need a controller and as always I have a base class for it. Like this I am sure to inherit from everything I need and I can add some extra common code if needed.
You can notice that I have coded the path to the view this is not required but I don't use the path by default because I want to have a separated folder for all my blocks.
To be able to drop those blocks into the page you need to add a ContentArea field in your page type and display it into the view of this page: First you need to add a field like this into the page:
You can find the other post of this series here:
Of course this is very similar to the creation of a page
In place of inheriting from PageType you should inherit from BlockData now. And I have of course always a base class like this I am able to add some common code to all my custom block data. My base class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EPiServer.Core;
namespace MyNameSpace.BlocksTypes.Base
{
public class BaseBlockType : BlockData
{
}
}
My Class for a richtext bloc:
using System.ComponentModel.DataAnnotations;
using EPiInno.BOL.BlocksTypes.Base;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
namespace MyNamespace.BOL.BlocksTypes
{
[ContentType(GroupName = SystemTabNames.Content,
DisplayName = "Richtext block",
Description = "A richtext block",
AvailableInEditMode = true,
Order = 2,
GUID = "{CF3D7201-9E5F-48C8-B3AF-7BA3BBD7E662}")]
public class RichTextBlock : BaseBlockType
{
[CultureSpecific]
[Display(
Name = "The body",
Description = "The body.",
GroupName = SystemTabNames.Content,
Order = 1)]
public virtual XhtmlString Body { get; set; }
}
}
As in the page type, we need a controller and as always I have a base class for it. Like this I am sure to inherit from everything I need and I can add some extra common code if needed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EPiServer.Core;
using EPiServer.Web;
using EPiServer.Web.Mvc;
namespace MyNamespace.BOL.Base.Controllers
{
public class BaseBlockController<T> : ActionControllerBase, IRenderTemplate<T>, IRenderTemplate where T : BlockData
{
}
}
The richtext block controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyNamespace.BOL.Base.Controllers;
using MyNamespace.BOL.BlocksTypes;
using MyNamespace.MVC4.ViewModels.Blocks;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Framework.Web;
using EPiServer.ServiceLocation;
using EPiServer.Web;
using EPiServer.Web.Mvc;
using EPiServer.Web.Routing;
namespace Mynamespace.Controllers.Blocks
{
[TemplateDescriptor(
AvailableWithoutTag = true,
Tags = new[] { RenderingTags.Preview })]
public class RichTextBlockController : BaseBlockController<RichTextBlock>
{
public ActionResult Index(RichTextBlock currentBlock)
{
return View("~/Views/Blocks/RichTextBlock/Index.cshtml", currentBlock);
}
}
}
You can notice that I have coded the path to the view this is not required but I don't use the path by default because I want to have a separated folder for all my blocks.
And the view for the controller:
@model MyNamespace.BlocksTypes.RichTextBlock
<div class="block richtext_block">
@Html.PropertyFor(x => x.Body)
</div>
To be able to drop those blocks into the page you need to add a ContentArea field in your page type and display it into the view of this page: First you need to add a field like this into the page:
[Display(
Name = "Blocks",
Description = "The blocks",
GroupName = TabNames.Content,
Order = 99)]
public virtual ContentArea Blocks { get; set; }
And into the view of this page just render the field with a PropertyFor like this:
@Html.PropertyFor(x => x.Blocks)

