Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark Watson 118 posts 384 karma points
    Jul 04, 2018 @ 02:22
    Mark Watson
    0

    2nd drop down menu value not working

    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;
    
  • jf 8 posts 103 karma points c-trib
    Jul 04, 2018 @ 08:10
    jf
    1

    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.

  • Mark Watson 118 posts 384 karma points
    Jul 04, 2018 @ 23:55
    Mark Watson
    0

    The code works for this. The reason for the errors was the member field was left blank and did not have a value.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 18, 2018 @ 14:07
    Alex Skrypnyk
    0

    Hi Mark

    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

    Thanks,

    Alex

  • Mark Watson 118 posts 384 karma points
    Jul 19, 2018 @ 06:30
    Mark Watson
    0

    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?

Please Sign in or register to post replies

Write your reply to:

Draft