Type True/False in Umbraco

3.9k views Asked by At

We want to implement a check box[Type : true/false] in an DocumemtType in Umbraco.

Our current Project necessity is:

an check box which will decide whether an image should be an link or popup

The code goes this way ...

    var child= @Model;

    if(child.GetProperty("popUp").Value.ToString() == "1")
      {
        // true means image will act as popup
      }
     else
      {
         // false means image will act as link
      }

But the problem is an error is occurred "Cannot perform runtime binding on a null reference"

I have also tried code like ,

      if (child.GetProperty("popup").Value.Equals("1"))
             {

             }

or

      if (child.GetProperty("popup").Value.ToString().Equals("1"))
             {

             }

but still not able to get it. All suggestions are welcomed .

4

There are 4 answers

0
Swapneel Kondgule On BEST ANSWER

Used the below code and it worked fine for me

var child= @Model;

if(@child.popUp)
  {
    // true means image will act as popup
  }
 else
  {
     // false means image will act as link
  }
1
Martijn van der Put On

Use this:

var child= @Model;

if(child.GetPropertyValue<bool>("popUp", false))
  {
    // true means image will act as popup
  }
 else
  {
     // false means image will act as link
  }
0
Dima Stefantsov On

node.GetProperty("popUp") is the way to go. If your control value is actually string, then your check logic would look like

if (node.GetProperty<string>("popUp") == "1"){}

Effectively generic GetProperty is what your code does, but it handles the null case, returning default(string).

(I have never used the dynamic thing, in case something will go wrong there, do the typed var node = new Node(id);)

0
Douglas Ludlow On

Since you recently added the property to the document type, unless each node of that type has been published, the property will return null. You'll need to check if the property is null first then check if its true.

var popUp = child.GetProperty("popUp");
if (popUp != null && popUp.Value.Equals("1"))
{
    // popup...
}
else
{
    // link...
}