get choice type data from Module.
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,
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];
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'
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];
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];
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.
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];
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!
glad you got it resolved, if you have any other issues don't hesitate to post or open a ticket :)
thank! very good solution :)
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,
Thanks Bishoy.
It really helps.I used following to extract data
lblSubmission.Text = objContent.GetValue<ChoiceOption>("Submission").Text;
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[].
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()