I am working in Umbraco cloud and have created a partial view with the following;
@foreach (var member in ApplicationContext.Current.Services.MemberService.GetMembersByMemberType("activeMembers"))
{
var memberStateDataType = Umbraco.DataTypeService.GetDataTypeDefinitionByName("Active Members - Practice 1 State - Dropdown");
var memberStateDictionary = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(memberStateDataType.Id).PreValuesAsDictionary;
var prac1name = member.GetValue("practice1Name");
var prac1ad = member.GetValue("practice1Address");
var prac1pc = member.GetValue("practice1Postcode");
var prac1ph = member.GetValue("practice1Phone");
var prac1fax = member.GetValue("practice1Fax");
var prac2name = member.GetValue("practice2Name");
var prac2ad = member.GetValue("practice2Address");
var prac2pc = member.GetValue("practice2Postcode");
var prac2ph = member.GetValue("practice2Phone");
var prac2fax = member.GetValue("practice2Fax");
var stateProperty1 = member.Properties.FirstOrDefault(p => p.PropertyType.Alias == "practice1State");
var selectedPreValueId1 = int.Parse((string)stateProperty1.Value);
var selectedPreValue1 = memberStateDictionary.FirstOrDefault(preValue => preValue.Value.Id == selectedPreValueId1);
// next line is the state e.g. "NSW"
var selectedState1 = selectedPreValue1.Value.Value;
This works for the first drop down menu but when I try to duplicate this for the second drop down menu I keep getting the yellow screen. Have tried a few variations but I am stuck. Code for the second drop down below:
@foreach (var member in ApplicationContext.Current.Services.MemberService.GetMembersByMemberType("activeMembers"))
{
var memberStateDataType = Umbraco.DataTypeService.GetDataTypeDefinitionByName("Active Members - Practice 1 State - Dropdown");
var memberStateDictionary = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(memberStateDataType.Id).PreValuesAsDictionary;
var memberStateDataType2 = Umbraco.DataTypeService.GetDataTypeDefinitionByName("Active Members - Practice 2 State - Dropdown");
var memberStateDictionary2 = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(memberStateDataType2.Id).PreValuesAsDictionary;
var prac1name = member.GetValue("practice1Name");
var prac1ad = member.GetValue("practice1Address");
var prac1pc = member.GetValue("practice1Postcode");
var prac1ph = member.GetValue("practice1Phone");
var prac1fax = member.GetValue("practice1Fax");
var prac2name = member.GetValue("practice2Name");
var prac2ad = member.GetValue("practice2Address");
var prac2pc = member.GetValue("practice2Postcode");
var prac2ph = member.GetValue("practice2Phone");
var prac2fax = member.GetValue("practice2Fax");
var stateProperty1 = member.Properties.FirstOrDefault(p => p.PropertyType.Alias == "practice1State");
var selectedPreValueId1 = int.Parse((string)stateProperty1.Value);
var selectedPreValue1 = memberStateDictionary.FirstOrDefault(preValue => preValue.Value.Id == selectedPreValueId1);
// next line is the state e.g. "NSW"
var selectedState1 = selectedPreValue1.Value.Value;
var stateProperty2 = member.Properties.FirstOrDefault(p => p.PropertyType.Alias == "practice2State");
var selectedPreValueId2 = int.Parse((string)stateProperty2.Value);
var selectedPreValue2 = memberStateDictionary2.FirstOrDefault(preValue => preValue.Value.Id == selectedPreValueId2);
// next line is the state e.g. "NSW"
var selectedState2 = selectedPreValue2.Value.Value;
I'm guessing one of the variables returned from FirstOrDefault. Paste the output from the yellow screen and we can be sure.
Just an aside, it may be worth refactoring the code a little so that you only retrieve the DataType definitions and PreValues outside the foreach, at the moment you will make those calls to the DataTypeService for every member and don't need to because those values won't change between users.
Nice that you found a solution. Please, mark the topic as solved :)
Also, move this code:
var memberStateDataType = Umbraco.DataTypeService.GetDataTypeDefinitionByName("Active Members - Practice 1 State - Dropdown");
var memberStateDictionary = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(memberStateDataType.Id).PreValuesAsDictionary;
var memberStateDataType2 = Umbraco.DataTypeService.GetDataTypeDefinitionByName("Active Members - Practice 2 State - Dropdown");
var memberStateDictionary2 = Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(memberStateDataType2.Id).PreValuesAsDictionary;
Out of the foreach, because of possible performance issue
I thought I had, but I need for the second drop down menu state selection to be left blank sometimes. If the second drop down menu state selection has a value it works if it is left blank it does not.
2nd drop down menu value not working
I am working in Umbraco cloud and have created a partial view with the following;
This works for the first drop down menu but when I try to duplicate this for the second drop down menu I keep getting the yellow screen. Have tried a few variations but I am stuck. Code for the second drop down below:
What error are you getting?
I'm guessing one of the variables returned from FirstOrDefault. Paste the output from the yellow screen and we can be sure.
Just an aside, it may be worth refactoring the code a little so that you only retrieve the DataType definitions and PreValues outside the foreach, at the moment you will make those calls to the DataTypeService for every member and don't need to because those values won't change between users.
The code works for this. The reason for the errors was the member field was left blank and did not have a value.
Hi Mark
Nice that you found a solution. Please, mark the topic as solved :)
Also, move this code:
Out of the foreach, because of possible performance issue
Thanks,
Alex
Thanks Alex
I thought I had, but I need for the second drop down menu state selection to be left blank sometimes. If the second drop down menu state selection has a value it works if it is left blank it does not.
Any ideas on how to fix this?
is working on a reply...