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;
}
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
Hi Martin,
In the case of nothing selected, would checking for empty be a case?
Something like:
/B.
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
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:
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.
is working on a reply...