2
votes

In my instance of Orchard, I have a custom content type. When creating an instance of the content type, a query string value must be passed to the editor page in order to set a value for the correlating model behind the scenes.

The problem is, as soon as "save" or "publish now" are hit, the query string is lost. It is not maintained in the URL and any references to the query string in the Driver return null.

So, is there any way to maintain the state of the query string?

Code sample:

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

Initially, "id" is set to the query string parameter, but after postback the query string return "null".

Note: I am using Orchard version 1.6.

1
No, there is no way to maintain the state of the querystring unless you take over everything: the controller, form rendering, etc. A better question would be why you want to maintain that on the querystring. Why is this not maintained as a property of the part and/or a hidden form field? What is this id exactly?Bertrand Le Roy

1 Answers

2
votes

You can get query string parameter on postback if you save it on a page in hidden field. If the edit shape depend on this parameter that it will be a little harder.

Driver:

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

Driver:

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
    ));
}

View

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