gpt4 book ai didi

asp.net - Orchard CMS - 确定模型在内容项驱动程序中是否有效

转载 作者:行者123 更新时间:2023-12-02 15:15:19 24 4
gpt4 key购买 nike

在我的 Orchard 实例中,我有一个自定义内容类型,其中有一个自定义内容部分。在内容部分的“编辑器驱动程序”中,我需要检查容器内容项是否有效(即通过验证)。

由于 Orchard 的工作方式,正常的 ModelState 在这里不起作用 - 我可以确定内容部分是否有效,但我需要了解整个内容项(内容项中还有其他内容部分)。

我知道使用生命周期事件 ( http://docs.orchardproject.net/Documentation/Understanding-content-handlers ) 发布/创建内容部分后,有多种方法可以执行代码,但(据我所知)没有办法传递这些事件信息。

基本上,如果内容项有效,我需要执行一个方法,并且需要传递 ViewModel 中包含的方法信息。

可能有(而且可能是)更好的方法来做到这一点,但我正在努力在 Orchards 框架内找到一种方法。

示例代码:

//POST
protected override DriverResult Editor(EventPart part, IUpdateModel updater, dynamic shapeHelper)
{
var viewModal = new EventEditViewModel();

if (updater.TryUpdateModel(viewModal, Prefix, null, null))
{
part.Setting = viewModal.Setting;
}

//here's where I need to check if the CONTENT ITEM is valid or not, for example
if (*valid*)
{
DoSomething(viewModal.OtherSetting);
}

return Editor(part, shapeHelper);
}

注意:我使用的是 Orchard 版本 1.6。

最佳答案

恐怕没有简单的方法可以从驱动程序内部做到这一点。太早了。您可以通过执行 part.As<OtherPart> 来访问其他部分,但目前这些可能会也可能不会更新。

您可以尝试使用处理程序和 OnPublishing/OnPublished (和其他)类似这样的事件:

        OnPublishing<MyPart>((ctx, part) =>
{
// Do some validation checks on other parts
if (part.As<SomeOtherPart>().SomeSetting == true)
{
notifier.Error(T("SomeSetting cannot be true."));
transactions.Cancel();
}
});

哪里transactionsITransactionManager实例,注入(inject)ctor中。

如果您需要更多控制,编写自己的 Controller 来处理项目更新/创建是最好的方法。

为了做到这一点(假设您已经有了 Controller ),您需要使用处理程序 OnGetContentItemMetadata方法来指示 Orchard 使用您的 Controller 而不是默认 Controller ,如下所示:

        OnGetContentItemMetadata<MyPart>((context, part) =>
{
// Edit item action
context.Metadata.EditorRouteValues = new RouteValueDictionary {
{"Area", "My.Module"},
{"Controller", "Item"},
{"Action", "Edit"},
{"id", context.ContentItem.Id}};

// Create new item action
context.Metadata.CreateRouteValues = new RouteValueDictionary {
{"Area", "My.Module"},
{"Controller", "Item"},
{"Action", "Create"});
});

关于asp.net - Orchard CMS - 确定模型在内容项驱动程序中是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14328141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com