Implode and Explode error on conversion from select2 multiple post

Implode and Explode error on conversion from select2 multiple post

ameji012ameji012 Posts: 8Questions: 2Answers: 0

I'm attempting to update using select2 multiple true option to post multiple values to the db but im running into conversion issues with the built ins. My option might be to convert the collection to a string using some lamda but i figured the built ins would work anyone else ran into this issue?

Form Data from datatables

data[1][RequestLine][SubstituteProducts][]
0:+2012
1:+1991
var response = new Editor(db, "RequestLine")
                    .Field(new Field("RequestLine.Id").Set(false))
                    .Model<RequestLineModel>("RequestLine")
                    .Field(new Field("SubstituteProducts")
                        .GetFormatter(Format.Explode())
                        .SetFormatter(Format.Implode())
                        )
                    ;

SubstituteProducts within the db is of type varchar and the model within aspx I declare it as a string

error

Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'System.String[]'.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,647Questions: 1Answers: 10,093 Site admin

    Easiest option is probably to use the separator option the Select2 plug-in for Editor has. Then it is all done client-side and no need to use formatters on the server-side.

    Allan

  • ameji012ameji012 Posts: 8Questions: 2Answers: 0

    Not sure how that would change the data type that is posted to the server. Are the implode and explode methods not suitable for collections?

  • ameji012ameji012 Posts: 8Questions: 2Answers: 0

    Even after setting the separator /tokenSeparator to true the result is still posted as a array.

    js

    minimumInputLength: 3,
    multiple: true,
    "tokenSeparators": ['|']
    
  • allanallan Posts: 61,647Questions: 1Answers: 10,093 Site admin
    Answer ✓

    Add separator: '|' in your field configuration (not inside the opts object). That will do the split / join on the client-side for you.

    Allan

  • ameji012ameji012 Posts: 8Questions: 2Answers: 0
    edited January 2020

    Thank You Allan,

    It seems that did the trick for posting to the server, but it does not like the name. I was required to do the below.

    FROM
    name: "RequestLine.SubstituteProducts"

    TO
    name: "RequestLine.SubstituteProducts[]"

    ALSO
    NEW LABEL w/ reassignments

    {
        label: "Substitute Products?",
        name: "RequestLine.SubstituteProducts",
        type: 'hidden'
    }
    
    //......
    {
                            label: "List of suitable substitute if this product is on back order?",
                            name: "PrfRequestLine.SubstituteProducts",
                            type: 'hidden'
                        },
    editor.on("preOpen", function () {
        var SubstituteProducts = editor.field("RequestLine.SubstituteProducts");
        if (SubstituteProducts.val()) {
            d = SubstituteProducts.val().split('|');
            editor.field("RequestLine.SubstituteProducts[]").update(d);
            editor.val("RequestLine.SubstituteProducts[]", SubstituteProducts.val());
        }
    });
    
  • allanallan Posts: 61,647Questions: 1Answers: 10,093 Site admin

    Interesting - thank you. We'll take a look at the server-side aspect of this as well as that should work...

    Allan

This discussion has been closed.