I have a question I have been struggling a bit with the last days. I'm trying to understand how the rendering of blocks works in Episerver 7 when working with MVC.
My current setup that actually works is as follows:
I have created a Block Type called "TeaserBlock" which has a couple of properties:
[ContentType(DisplayName = "TeaserBlock", GUID = "571582c4-6b99-4c0a-b000-f62265e312a9", Description = "A Teaser Block for show!")]
public class TeaserBlock : BlockData
{
[Display( GroupName = SystemTabNames.Content, Order = 1)]
public virtual string TeaserHeading { get; set; }
[Display(GroupName = SystemTabNames.Content, Order = 2)]
public virtual string TeaserText { get; set; }
}
To this I have a corresponding partial view in Views/Shared/ called TeaserBlock.cshtml. This one renders the block in view mode I guess?
My biggest problem was to get the "on page edit" to work and after some trying and googling I finally got it to work with the following code:
[TemplateDescriptor(Inherited = true,
Tags = new string[] { RenderingTags.Preview },
TemplateTypeCategory = TemplateTypeCategories.MvcController)]
public class PreviewBlockController : ActionControllerBase, IRenderTemplate<BlockData>
{
public ActionResult Index(BlockData currentBlock)
{
return View(currentBlock);
}
}
After I added a corresponding view to the PreviewBlockController it worked in "on page edit" which was good. It took me quite a while before I realized if the line "TemplateTypeCategory = TemplateTypeCategories.MvcController)" was not there it did not work. Anyone knows why this line is necessary?
Also is there some magic involved when using the Tag "RenderingTags.Preview" ?
Let's say that I have 5 different blocks in my application do I then need 5 different preview controllers and corresponding views to them as well?
What I would like is to have a more general "block preview controller" that could handle all "on page edit" for my blocks. I'm not sure if this is possible however because all Blocks will have different properties and therefore needs individual .cshtml for their preview if the preview will be any meaningful I guess. Or is there some way for me in code to find out what properties that is relevant in the block sent it to my controller so that I can loop over them for example? And with the help of this only need one preview controller and a corresponding view?
Is there any guidelines on how to deal with this in the best way?
Hope anyone can help me understand this concept a bit better, I find it hard to find information for some concepts regarding episerver.