如何在 EPiServer 8 中显示计算出的 属性 值?

How to show computed property values in EPiServer 8?

在页面编辑模式下,我想显示基于页面 属性 值的只读文本。例如,文本可以是 "A content review reminder email will be sent 2015-10-10",其中日期基于页面发布日期 + 六个月(一个可配置的值,因此可以随时更改)。到目前为止,我已经尝试通过在页面上添加另一个 属性 来完成类似的事情。

我已将 属性 CurrentReviewReminderDate 添加到我们使用的 InformationPage class 中。在页面编辑模式下,显示 属性 名称,但它没有值。如何在页面编辑模式下显示值(最好是作为标签)?

[CultureSpecific]
[Display(
    Name = "Review reminder date",
    Description = "On this date a reminder will be sent to the selected mail to remember to verify page content",
    Order = 110)]
[Editable(false)]
public virtual string CurrentReviewReminderDate
{
    get
    {
        var daysUntilFirstLevelReminder =
            int.Parse(WebConfigurationManager.AppSettings["PageReviewReminder_DaysUntilFirstLevelReminder"]);
        if (CheckPublishedStatus(PagePublishedStatus.Published))
        {
            return StartPublish.AddDays(daysUntilFirstLevelReminder).ToString();
        }
        return "";
    }
    set
    {
        this.SetPropertyValue(p => p.CurrentReviewReminderDate, value);
    }
}

EPiServer 在为 UI 检索内容时在内部使用 GetPropertyValue 方法(即 SetPropertyValue 的相反方法)。 =10=]

这是有道理的,否则无论何时保存内容,您的 "made-up" 值都会存储为实际值。 这将使回退值等无法实现

所以,这是 EPiServer 的设计(而且非常明智)。 :)

但是,您可以通过以下方式自定义属性的工作方式:

  1. 通过应用 UI 提示使用自定义编辑器
  2. 修改 属性 元数据(例如,将生成的值显示为文本框中的水印,而不干扰保存的实际值)

我可能误解了你想做什么,但在我的脑海中,自定义编辑器似乎是你用例的可行选择?

另一个解决方案是连接到 LoadedPage 事件并从那里添加值。这可能不是性能方面的最佳方式,因为您需要执行 CreateWritableClone,但根据网站的不同,这可能无关紧要。

    [InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class EventInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        ServiceLocator.Current.GetInstance<IContentEvents>().LoadedContent += eventRegistry_LoadedContent;
    }

    void eventRegistry_LoadedContent(object sender, ContentEventArgs e)
    {
        var p = e.Content as EventPage;
        if (p != null)
        {
            p = p.CreateWritableClone() as EventPage;
            p.EventDate = p.StartPublish.AddDays(10);
            e.Content = p;
        }
    }
}