What does the form prefix part do in an editor method?

using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;

namespace Orchard.Webshop.Drivers {
    public class ProductDriver : ContentPartDriver<ProductPart> {
        protected override DriverResult Editor(ProductPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_Product_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/Product", Model: part, Prefix: Prefix));
        }

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

      

I've searched for what Prefix does , but couldn't find anything that identifies it or explains its purpose. And if the prefix can be used in methods other than Editor, please feel free to state it as well. Thank.

+3


source to share


1 answer


It is a string prefix that is added to form field names in editor templates. The reason why this is necessary:

Let's say you've created a new piece of content named NoobsPart with a Text property. And in the "N00b" content type that contains the N00bsPart, you also attach the BodyPart. BodyPart also has a Text property. The field names in the HTML file BodyPart and N00bsPart would collide with each other on postback. The prefix solves the field name collision problem. Typically, a prefix can be set on a part name to disambiguate and avoid collisions. I'm not sure if the prefix is ​​being used anywhere else, but given the problem it solves, I wouldn't think it.



The following examples in Orchard core code typically set a prefix as a property of a class in a driver:

using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;

namespace Orchard.Webshop.Drivers {
    public class ProductDriver : ContentPartDriver<ProductPart> {
        protected override string Prefix { get { return "ProductPart"; } }
        protected override DriverResult Editor(ProductPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_Product_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/Product", Model: part, Prefix: Prefix));
        }

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

      

+8


source







All Articles