+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / get choice type data from Module.

get choice type data from Module.

14 posts, 1 answered
  1. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    08 May 2012
    Link to this post

    I have a custom module which has a choice type enabled, so people can click the radio buttons and select an input.
    That works fine but when I try and get the value from the Collection the output I get in my array is
    System.String[]

    My code for getting the value is:

    buildingLocationType = item.GetValue("locationType").ToString();
    buildingArraryList.Add(buildingLocationType);
    HiddenType.Value = buildArrayToString(ref buildingArraryList);

     

    I have already retrieved other information from the module with a similiar method and it works but it just seems to be the choice type that is causing me an issue,

  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    08 May 2012
    Link to this post
    Owain, choice fields are indeed persisted as arrays of string. If a single item is chosen, the value is an array with a length of 1, the single element holding the value.

    Cast the value to a string[] and you should be able to retrieve the value by index (be sure to check for nulls):

    buildingLocationType = (string[])item.GetValue("locationType");
    HiddenType.Value = buildingLocationType[0];

    I know that the team is working on improving this experience, I'll keep an eye on this a blog if the situation changes. in the meantime I hope this is helpful!
  3. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    08 May 2012
    Link to this post
    Thanks for the reply but I am getting an error:
    I am needing to output the data to my own array and then I match the information to bring it back in to a javascript function. The error I get is:
    CS0029: Cannot implicitly convert type 'string[]' to 'string'
  4. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    08 May 2012
    Link to this post
    This code may help :

    foreach (var item in myCollection)
               {
                   Counter++;
                   
                    
                   Latitude = item.GetValue("crtLatitude").ToString();
                   LatArrayList.Add(Latitude);
                   HiddenLat.Value = ArrayListToString(ref LatArrayList);
     
                   Longitude = item.GetValue("crtLongitude").ToString();
                   LonArrayList.Add(Longitude);
                   HiddenLon.Value = LonArrayListToString(ref LonArrayList);
     
                  
     
                   Location = item.GetValue("nameOfCourt").ToString();
                   LocationArrayList.Add(Location);
                   HiddenLocation.Value = LocationArrayToString(ref LocationArrayList);
     
     
                   buildingLocationType = (string[])item.GetValue("locationType");
                   //buildingArraryList.Add(buildingLocationType);
                   HiddenType.Value = buildingLocationType[0];
     
     
                }

    As I said in my previous post, I am getting an error when trying to get the information out for the choice field.
  5. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    08 May 2012
    Link to this post
    it appears the buildingLocationType is a existing variable of type string. try declaring a new variable so that it takes on the correct string[] type:

    var locationType = (string[])item.GetValue("locationType");
    buildingArraryList.Add(locationType[0]);
    HiddenType.Value = locationType [0];
  6. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    09 May 2012
    Link to this post
    Thanks for your continued help. Tried your code and now get

    Exception Details:
    System.IndexOutOfRangeException: Index was outside the bounds of the array.

    So annoying when all I want to do is take the contents of the module field, save it in to a new array, output it to a hidden literal which I then read in to my javascript when required. As I say, I've managed to do this on standard data but the choice field seems to be causing nothing but issues for me.
  7. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    09 May 2012
    Link to this post
    hi owain, be sure you are doing a check for nulls or empty values, as the array will be empty if no choice was selected.

    var locationType = (string[])item.GetValue("locationType"); 
    if (locationType.Length > 0)
    {
         buildingArraryList.Add(locationType[0]); 
         HiddenType.Value = locationType [0];
    }

    should avoid the error. hope this works for you!
    Answered
  8. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    11 May 2012
    Link to this post
    Thanks for your continued help with this but after checking to see if the array is empty or not I still get the error "Index was outside the bounds of the array. "
    EDIT: I was uploading from the wrong version! Its now checking the array. Sorry!
  9. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    12 May 2012
    Link to this post
    glad you got it resolved, if you have any other issues don't hesitate to post or open a ticket :)
  10. Francisco
    Francisco avatar
    7 posts
    Registered:
    14 Sep 2010
    23 Oct 2012 in reply to SelAromDotNet
    Link to this post
    thank! very good solution :)
  11. Bishoy
    Bishoy avatar
    27 posts
    Registered:
    15 Aug 2013
    28 Aug 2013
    Link to this post
    To Get the Value Selected use ChoiceOption or Telerik.Sitefinity.DynamicModules.Model.ChoiceOption
    QuestionTypeID = int.Parse(((ChoiceOption)parent.SystemChildItems.FirstOrDefault().GetValue("QestionType")).PersistedValue),
    QuestionTypeName = ((ChoiceOption)parent.SystemChildItems.FirstOrDefault().GetValue("QestionType")).Text,
  12. kamii47
    kamii47 avatar
    71 posts
    Registered:
    02 Mar 2007
    11 Apr 2014 in reply to Bishoy
    Link to this post

    Thanks Bishoy.

    It really helps.I used  following to extract data

    lblSubmission.Text = objContent.GetValue<ChoiceOption>("Submission").Text;

  13. Steve
    Steve avatar
    1 posts
    Registered:
    27 Feb 2009
    31 Jan 2015
    Link to this post

    Quick answer you need to cast to IEnumerable in 7.2- foreach (ChoiceOption selectedValue in (System.Collections.Generic.IEnumerable<ChoiceOption>)Checkoptions)

     

    Thanks every one for the responses.  It appears in 7.2 there is a typing issue for multiple ChoiceOptions.  Textbox and Checkbox work fine but not Choices.

    The sitefinity module builder example suggests: 

    var stringArray = pressReleaseItem.GetValue<string[]>("Multiple");

    That will throw an exception of "Unable to cast object type of
    Telerik.Sitefinity.DynamicModules.Model.ChoiceOption[]' to type 'System.String'

    I solved this by casting the value to iEnumerated.

    SetSelectedCheckBoxListObject("checkboxlistName", pressReleaseItem.GetValue("Multiple"));

     

    public void SetSelectedCheckBoxListObject(string CheckBoxName, object Checkoptions)

            {
                try
                {
                    CheckBoxList checkBoxList = (CheckBoxList)FindControl(CheckBoxName);
                    string strValue;
                    foreach (ChoiceOption selectedValue in (System.Collections.Generic.IEnumerable<ChoiceOption>)Checkoptions)
                    {
                        strValue = selectedValue.PersistedValue;
                        checkBoxList.Items.FindByValue(strValue).Selected = true;
                    }

                }
                catch (Exception ex)
                {
                    //return value;
                }
            }

    I hope this helps and eliminates the long debug process I had did to solve the problem.

    In the future I suggest sitefinity 7.2 adjusts the ChoiceOption to inherit from System.Collections.Generic.IEnumerable so it can be directly cast to System.String[].




                                 
                   

  14. Murli
    Murli avatar
    4 posts
    Registered:
    18 Nov 2015
    26 Feb in reply to kamii47
    Link to this post

    Your code works for me. I was struggling since two days to get the Dynamic dropdown value

     I have used like this may be this will help to other..

     List<MyDropDown> list = new List<MyDropDown>();
                    string Title = string.Empty;
                    string Value = string.Empty;
                    foreach (var item in myCollection)
                    {
                        Value = ((ChoiceOption)item.GetValue("FinancialYear")).PersistedValue;
                        Title = ((ChoiceOption)item.GetValue("FinancialYear")).Text;
                        list.Add(new MyDropDown() { DropdownTitle = Title, DropdownValue = Value });
                    }

     

    Note: 1.  FinancialYear is the dynamic dropdown control id

         2. MyDropDown is my custom class to binding dropdown like this

        public class MyDropDown {
            public string DropdownTitle { get; set; }
            public string DropdownValue { get; set; }
            public MyDropDown() { }
        }

     

14 posts, 1 answered