【问题标题】:Orchard CMS - Maintain query string value in content item editorOrchard CMS - 在内容项编辑器中维护查询字符串值
【发布时间】:2013-01-11 16:22:19
【问题描述】:

在我的 Orchard 实例中,我有一个自定义内容类型。创建内容类型的实例时,必须将查询字符串值传递给编辑器页面,以便在后台为关联模型设置值。

问题是,只要点击“保存”或“立即发布”,查询字符串就会丢失。它不在 URL 中维护,对驱动程序中查询字符串的任何引用都返回 null。

那么,有没有什么办法可以保持查询字符串的状态呢?

代码示例:

//GET
protected override DriverResult Editor(PerformerPart part, dynamic shapeHelper)
{
    var workContext = _workContextAccessor.GetContext();
    var request = workContext.HttpContext.Request;
    var id = request.QueryString["id"];
}

最初,“id”设置为查询字符串参数,但回发后查询字符串返回“null”。

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

【问题讨论】:

  • 不,除非您接管一切:控制器、表单渲染等,否则无法维护查询字符串的状态。一个更好的问题是您为什么要在查询字符串上维护它。为什么这不作为部件和/或隐藏表单字段的属性进行维护?这个 id 到底是什么?

标签: asp.net orchardcms orchardcms-1.6


【解决方案1】:

如果您将其保存在隐藏字段中的页面上,您可以在回发时获取查询字符串参数。 如果编辑形状依赖于这个参数会有点困难。

司机:

protected override DriverResult Editor(PerformerPart part, dynamic shapeHelper)
{
    return Editor(part, null, shapeHelper);
}

司机:

protected override DriverResult Editor(PerformerPart part, IUpdateModel updater, dynamic shapeHelper)
{
    var model = new PerformerPartEditViewModel();

    if (updater != null)
    {
        if (updater.TryUpdateModel(model, Prefix, null, null))
        {
            // update part
        }
    }
    else
    {
        model.StrId = _wca.GetContext().HttpContext.Request.QueryString["id"]; // if you save id in your part that you can also try get it from the part
    }

    if (string.IsNullOrEmpty(model.StrId))
    {
        // populate model with empty values
    }
    else
    {
        // populate model with right values
    }

    return ContentShape("Parts_Performer_Edit", () => shapeHelper.EditorTemplate(
            TemplateName: "Parts/Performer",
            Prefix: Prefix,
            Model: model
    ));
}

查看

@model Smth.ModuleName.ViewModels.PerformerPartEditViewModel
@Html.HiddenFor(m => m.StrId)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多