Show posted data in XForm form with MVC

Vote:
 

I can't seem to find a way to show previously posted data in an XForm form for MVC. Following description is given by EPiServer:

"It is possible to re-edit data that is posted to the database (or another custom location as long as you can load and save the XFormData object). To do this, create an XFormControl object and set its Data property to the XFormData object you want to load. The form will be loaded with the values from the posted data"

But since XFormControl is an webform control, I guess it needs to be taken care of differently in MVC. I got my XFormData object from DDS with all correct values, and I have loaded that object in my model, but can't find a suitable method to load it for my XForm, following methods is given in XFormExtensions:

http://world.episerver.com/Documentation/Class-library/?documentId=cms/7/91e81e54-6baf-80ea-bfca-b7f0c4a32432

Closest I can find is BeginXForm(xformposteddata), but the object I get from following line:

IList formData = form.GetPostedData();

Is an list of XFormData, which I assume is correct. I must have the possibility to go back to whatever form to fill in posted data, not just the parameter XFormPostedData that I get in the postback for a form.

I can of course taking care of this manually with either JQuery or in the backend and loop through all XML and then set correct values for the generated HTML in the XForm, but it should be supported to just connect the XFormData object to my XForm? In case anyone did something similar, suggestions on best practice?

/J

#88934
Aug 04, 2014 15:39
Vote:
 

Since I couldn't find a solution I handled it manually by following code:

In my posted method in the controller I added following code for setting the value for the checkbuttons and radiobuttons as a custom value in my XFormData object:

// Check if the answer is correct, all correct option got the value 1
            foreach (XFormsFragment fragment in xFormpostedData.XFormFragments)
            {
                // Radiobutton list
                if (fragment.GetType() == typeof(EPiServer.XForms.Parsing.Select1AsRadiobuttonFragment))
                {
                    EPiServer.XForms.Parsing.Select1AsRadiobuttonFragment fragmentObject = (EPiServer.XForms.Parsing.Select1AsRadiobuttonFragment)fragment;
                    IList options = fragmentObject.Options;

                    foreach (SelectOption option in options)
                    {
                        if (option.SelectedItem)
                        {
                            formData.SetValue("RadioButtonValue", option.Value);                                                    
                        }
                    }
                }
                // Checkbox list
                else if (fragment.GetType() == typeof(EPiServer.XForms.Parsing.SelectFragment))
                {
                    EPiServer.XForms.Parsing.SelectFragment checkBoxList = (EPiServer.XForms.Parsing.SelectFragment)fragment;
                    IList checkboxOptions = checkBoxList.Options;
                    var test = ConcatenateCheckBoxListValues(checkboxOptions);
                    formData.SetValue("CheckBoxList", ConcatenateCheckBoxListValues(checkboxOptions));                     
                }
            }

private string ConcatenateCheckBoxListValues(IList options)
        {
            StringBuilder sb = new StringBuilder();
            char[] characters = new char[] { ' ', ',' };
            String trimmedString;

            foreach (SelectOption option in options)
            {
                if (option.SelectedItem)
                {
                    sb.Append(option.Value + ", ");
                }
            }

            trimmedString = sb.ToString().TrimEnd(characters);
            return trimmedString;
        }

And in the Index method when loading a form with posted data:

// Load existing formdata if exists for current user
            XFormData currentUserXFormData = GetPostedDataForCurrentUser(currentPage.Form);

public XFormData GetPostedDataForCurrentUser(XForm form)
        {
            IList formData = form.GetPostedData();

            foreach (XFormData data in formData)
            {      

                // Will max be on hit
                if (data.UserName == HttpContext.User.Identity.Name)
                {
                    return data;                   
                }
            }

            return new XFormData();            
        }

And then I return desired value to my model in the Index method:

currentPage.XFormSavedValues = valuesToCheck;  // The values that is !string.isnullorempty from custom data from currentUserXFormData 

And then I checked checkboxes and radiobuttons with Jquery in my view as follows:

$(document).ready(function () {      

        var valueToCheck = "@Model.CurrentPage.XFormSavedValues";

        //Function to check the checkboxes with the saved values
        var valuesArray = valueToCheck.split(',');
        var index = 1;
        $('input[type=checkbox]').each(function () {
            if ($.inArray("" + index + "", valuesArray) >= 0) {
                $(this).attr('checked', 'checked');
            }
            index++;
        });

        //Function to check the radiobutton with the saved value
        if (valueToCheck.indexOf(',') == -1) {
            $(":radio[value=" + valueToCheck + "]").attr('checked', 'checked');
        }
        
    });

/ Johannes

#88992
Aug 05, 2014 15:31
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.