Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Jun 17, 2016 @ 08:25
    Martin
    0

    Switch Statement with Dropdown Property

    Hi,

    I'm having some trouble with a switch statement using the a dropdown property.

    Im getting an error when the dropdown has nothing selected, with following.

    "System.NullReferenceException: Object reference not set to an instance of an object"

    Any ideas on how I can show the default layout when the dropdown is null.

    Thanks

    Martin

    @{
    var layout = Model.GetPropertyValue("layout").ToString();
    }
    
        @switch (layout) {
            case "Layout One":
                <p>This is layout one</p>;
                break;
            case "Layout Two":
                <p>This is layout two</p>;
                break;
            case "Layout Three":
                <p>This is layout 3</p>;
                break;
            default:
                <p>This is the default layout</p>;
                break;
            }
    
  • Bijesh Tank 192 posts 420 karma points
    Jun 17, 2016 @ 10:44
    Bijesh Tank
    0

    Hi Martin,

    In the case of nothing selected, would checking for empty be a case?

    Something like:

    case "":
    

    /B.

  • Martin 278 posts 662 karma points
    Jun 17, 2016 @ 10:56
    Martin
    0

    Hi Bijesh, Thanks for replying.

    I've tried your suggestion with no success.

    I dont think the switch like the empty null value when nothing is selected in the dropdown.

    M

  • Bijesh Tank 192 posts 420 karma points
    Jun 17, 2016 @ 11:12
    Bijesh Tank
    0

    Ah I see what you mean. I just tried it myself, added a dropdown list to my doctype (called Test Dropdown) and added 2 values to it: Test 1, Test 2.

    Now referencing the umbraco.config file, when I select an item from the dropdown it will add the following to my node:

    <testDropdown><![CDATA[Test 1]]></testDropdown>
    

    However, when I select no value, then it totally removes the line, hence the NullReference exception.

    You could make this field mandatory in the backoffice so something has to be selected. Or in code check to see if the model is null.

    B.

Please Sign in or register to post replies

Write your reply to:

Draft