Orchard如何访问Controller中的Model部分

时间:2015-02-21 09:27:39

标签: asp.net asp.net-mvc orchardcms orchardcms-1.8

我有一个包含可靠的模型。我想在控制器中访问该证书,但我没有运气。谁能告诉我怎么做到这一点?

我的模特:

public class Interspire_MailingPart : ContentPart<Interspire_MailingPartRecord>
{
    [Required]
    public String SubscribeFormID
    {
        get { return Retrieve(r => r.SubscribeFormID); }
        set { Store(r => r.SubscribeFormID, value); }
    }
}

public class Interspire_MailingPartRecord : ContentPartRecord
{
    public virtual String SubscribeFormID { get; set; }
}

我在添加小部件时设置它们。我的迁移和一切都很好。

编辑: 澄清我想要做的事情: 我希望我的模块有两种不同的设置。一个将在站点级别,以便每个小部件将具有相同的。然后在小部件级别上,这样您添加的每个小部件都将具有不同的设置

我想在我的控制器中访问这两个设置。

但是我只能访问globalsetting但是localsetting会返回一个emptystring。

控制器

public class MailControllerWebApi : ApiController
{
    public MailControllerWebApi(IOrchardServices orchardServices)
    {
        Services = orchardServices;
        T = NullLocalizer.Instance;
     }

    public IOrchardServices Services { get; private set; }
    public Localizer T { get; set; }

    public string Get(string Email) {
        var localSettings = Services.WorkContext.CurrentSite.As<Interspire_MailingPart>();
        var globalSettings = Services.WorkContext.CurrentSite.As<Interspire_MailingSettingsPart>();

        return localSettings.SubscribeFormID;

    } 
}

两位处理程序:

public class MailingSettingsHandler: ContentHandler
{
    public MailingSettingsHandler(IRepository<Interspire_MailingSettingsPartRecord> repository)
    {
        T = NullLocalizer.Instance;
        Filters.Add(new ActivatingFilter<Interspire_MailingSettingsPart>("Site"));
        Filters.Add(new TemplateFilterForPart<Interspire_MailingSettingsPart>("Nastavitve", "Parts/MailingSettings", "Interspire"));
        Filters.Add(StorageFilter.For(repository));
    }

    public Localizer T { get; set; }

    protected override void GetItemMetadata(GetContentItemMetadataContext context) {
        if (context.ContentItem.ContentType != "Site")
            return;
        base.GetItemMetadata(context);
        context.Metadata.EditorGroupInfo.Add(new GroupInfo(T("Interspire")));
    }
}

public class MailingHandler: ContentHandler
{
    public MailingHandler(IRepository<Interspire_MailingPartRecord> repository)
    {
        T = NullLocalizer.Instance;
        Filters.Add(new ActivatingFilter<Interspire_MailingPart>("Site"));
        Filters.Add(StorageFilter.For(repository));
    }
    public Localizer T { get; set; }
}

和司机:

public class MailingDriver : ContentPartDriver<Interspire_MailingPart>
{
    private readonly IOrchardServices _services;

    public MailingDriver(INotifier notifier, IOrchardServices services)
    {
        _services = services;
    }

    protected override DriverResult Display(Interspire_MailingPart part, string displayType, dynamic shapeHelper)
    {
        var mailingSettings = _services.WorkContext.CurrentSite.As<Interspire_MailingSettingsPart>();
        return ContentShape("Parts_Mailing",
            () => shapeHelper.Parts_Mailing(
                Url: mailingSettings.Url,
                SubscribeFormID: part.SubscribeFormID
            ));
    }

    //GET
    protected override DriverResult Editor(Interspire_MailingPart part, dynamic shapeHelper)
    {
        return ContentShape("Parts_Mailing_Edit",
            () => shapeHelper.EditorTemplate(
                TemplateName: "Parts/Mailing",
                Model: part,
                Prefix: Prefix));
    }

    //POST
    protected override DriverResult Editor(Interspire_MailingPart part, IUpdateModel updater, dynamic shapeHelper)
    {
        updater.TryUpdateModel(part, Prefix, null, null);
        return Editor(part, shapeHelper);
    }
}

Globalsettings正常获取数据,但是localsetting只返回一个空字符串。

EDIT2 迁移文件:

public int Create() {

        ContentDefinitionManager.AlterTypeDefinition("InterspireMailingWidget", cfg => cfg
          .WithPart("Interspire_MailingPart")
          .WithPart("WidgetPart")
          .WithPart("CommonPart")
          .WithSetting("Stereotype", "Widget"));

        SchemaBuilder.CreateTable("Interspire_MailingPartRecord", table => table
            .ContentPartRecord()
            .Column("SubscribeFormID", DbType.String)
        );

        SchemaBuilder.CreateTable("Interspire_MailingSettingsPartRecord", table => table
             .ContentPartRecord()
             .Column("Url", DbType.String)
         );

        ContentDefinitionManager.AlterPartDefinition("Interspire_MailingPart",
          builder => builder.Attachable());

        return 1;
    }

1 个答案:

答案 0 :(得分:1)

你能告诉我们你的控制器代码吗? 通常你会注入IContentManager或从IOrchardServices中获取它并使用_contentManager.Get(...)或_contentManager.Query(...),检查出来。 谷歌,你会看到其他人如何做到这一点。