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 13, 2018 @ 02:08
    Mark Watson
    0

    drop down menu no selection causing error

    I am working in Umbraco cloud.

    The members can have 2 address details published on the web site with the following code.

    @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;
    

    This displays perfectly if the member has 2 address and "Active Members - Practice 2 State - Dropdown" has a value. However not all the members have 2 addresses. If the drop down menu "Active Members - Practice 2 State - Dropdown" does not have a value I get the yellow screen.

    I have tried @if (selectedState2 != null ) but i still get the same yellow screen.

    What am I missing?

Please Sign in or register to post replies

Write your reply to:

Draft