Search code examples
asp.net-mvc-5piranha-cms

Piranha CMS HtmlRegion within Custom Region not saving content


I've created a couple of custom regions within my Piranha CMS Installation but am having problems when I have any kind of text region within my custom region. The Editor will display and you can enter text but it doesn't save to the DB.

Here's my classes

using System;
using System.ComponentModel.Composition;
using Piranha.Extend;
using Piranha.Extend.Regions;

namespace MatchtechGroup.Models.Regions
{
[Export(typeof(IExtension))]
[ExportMetadata("InternalId", "SimpleTab")]
[ExportMetadata("Name", "Simple Tab")]
[ExportMetadata("Type", ExtensionType.Region)]
[Serializable]
public class SimpleTab : Extension, ITab
{
    public string Title { get; set; }
    public HtmlRegion Tab { get; set; }

    public SimpleTab()
    {
        Tab = new HtmlRegion();
    }
}
}

And my Manager template in Areas/Manager/Views/Extensions

@model MatchtechGroup.Models.Regions.SimpleTab
@{
    Layout = "";
}
<ul class="form">
    <li>
        @Html.LabelFor(m => m.Title)
        <div class="input">@Html.TextBoxFor(m => m.Title)</div>
    </li>
    <li>
        @Html.TextAreaFor(m => m.Tab, new { @class = "editor", @rows = 10 })
    </li>
</ul>

The manager interface renders my new region correctly in the page editor but will not save content from the Html Region. There are no errors displayed in the interface, I just don't get the 'This Page has saved' message bar appear or am I able to publish the page.

Any help would be much appreciated, feels like I'm missing something basic or just that I can't nest an HTML region within this custom region.

Thanks


Solution

  • The problem is probably that your HtmlValue gets invalidated in the Model Binder so that Model.IsValid is false. The easiest solution to your problem is to change your property to:

    public class SimpleTab : ...
    {
      public string Title { get; set; }
      public string Tab { get; set; }
    }
    

    The only difference would be when using it in the Razor markup. If you had a region of the SimpleTab type called MyTab the syntax would then be (for example):

    <div>
      <h3>@Model.Regions.MyTab.Title</h3>
      <div class="content">
        @Html.Raw(Model.Regions.MyTab.Tab)
      </div>
    </div>
    

    The only difference is @Html.Raw() to make sure that the body isn't escaped.

    Regards

    / Håkan