Orchard custom field not getting posted correctly

584 Views Asked by At

I have created a custom field in Orchard which is meant to just contain one field (GUID). So whenever it is added to a content type, it would show a new GUID in "Editor" template. This works fine, except when I submit the form which contains the content type, the form gets posted with a different GUID. On looking closely, I found the driver of the field for Editor (POST) does not updates the viewModel for the field.

Any suggestion or tips to debug ?

Here are some lines of my code

protected override DriverResult Display(
            ContentPart part, Fields.UniqueIDField field,
            string displayType, dynamic shapeHelper)
        {

            return ContentShape("Fields_String", // this is just a key in the Shape Table
                 GetDifferentiator(field, part),
                 () =>
                 {
                     var settings = field.PartFieldDefinition.Settings.GetModel<UniqueIdFieldSettings>();
                     var value = field.Id;


                     var viewModel = new UniqueIdFieldViewModel
                     {
                         Id = value
                     };

                     return shapeHelper.Fields_String( // this is the actual Shape which will be resolved (Fields/DateTime.cshtml)
                         Model: viewModel);
                 }
             );
        }

        protected override DriverResult Editor(ContentPart part,
                                               Fields.UniqueIDField field,
                                               dynamic shapeHelper)
        {

            var settings = field.PartFieldDefinition.Settings
                                .GetModel<UniqueIdFieldSettings>();
            var value = Guid.NewGuid().ToString();
            field.Id = value;

            var viewModel = new UniqueIdFieldViewModel
            {
                Id = value,
            };

            return ContentShape("Fields_String_Edit",
                () => shapeHelper.EditorTemplate(
                          TemplateName: TemplateName,
                          Model: viewModel,
                          Prefix: GetPrefix(field, part)));
        }

        protected override DriverResult Editor(ContentPart part,
                                               Fields.UniqueIDField field,
                                               IUpdateModel updater,
                                               dynamic shapeHelper)
        {

            var viewModel = new UniqueIdFieldViewModel();         
            if (updater.TryUpdateModel(viewModel,
                                       GetPrefix(field, part), null, null))
            {
                field.Id = viewModel.Id;
            }

            return Editor(part, field, shapeHelper);
        }
1

There are 1 best solutions below

0
On BEST ANSWER

You are resetting the value every time in the GET editor method, which is called by the POST editor method every time at the end of its own execution. Only set to a new value if there isn't one already.