Umbraco列表视图 - 如何验证

时间:2018-01-11 12:23:58

标签: asp.net asp.net-mvc umbraco umbraco7

我有一份翻译列表 - > id,isdefault,语言和内容。我想验证一下 1.用户保存时,应检查列表是否已包含相同的ID或语言? 2.如果用户选中了IsDefault,则应检查列表是否已为任何其他翻译选择了默认值。如果是,那么它应该显示消息,请取消选择IsDefault。

请建议。

1 个答案:

答案 0 :(得分:1)

可以使用 Saving 事件,通过 ApplicationEventHandler 继承类中的 ContentService 处理,如下所示:

using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace CustomerHomePage.Core.EventHandlers
{
    public class ApplicationHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saving += ContentServiceOnSaving;
        }

        private void ContentServiceOnSaving(IContentService sender, SaveEventArgs<IContent> saveEventArgs)
        {
            // Here should be any logic during saving of changes in any nodes
        }
    }
}

获取发件人的祖先并找到“翻译”列表,然后仅检查所有子元素值IsDefault。如果要取消保存并显示一些消息,请执行以下操作:

        if (e.CanCancel)
        {
            e.Cancel = true;
            e.CancelOperation(new EventMessage("Category name", "You message here", EventMessageType.Error));
        }