1

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

4 回答 4

1

node.GetProperty("popUp") 是要走的路。如果您的控制值实际上是字符串,那么您的检查逻辑将如下所示

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

有效的通用 GetProperty 是您的代码所做的,但它处理 null 情况,返回默认值(字符串)。

(我从来没有使用过动态的东西,如果那里出了问题,请输入 var node = new Node(id);)

于 2013-05-31T11:37:11.827 回答
1

由于您最近将该属性添加到文档类型,除非该类型的每个节点都已发布,否则该属性将返回 null。您需要先检查该属性是否为空,然后检查其是否为真。

var popUp = child.GetProperty("popUp");
if (popUp != null && popUp.Value.Equals("1"))
{
    // popup...
}
else
{
    // link...
}
于 2013-06-08T14:22:35.547 回答
1

使用了下面的代码,它对我来说很好用

var child= @Model;

if(@child.popUp)
  {
    // true means image will act as popup
  }
 else
  {
     // false means image will act as link
  }
于 2013-06-14T06:13:31.017 回答
0

用这个:

var child= @Model;

if(child.GetPropertyValue<bool>("popUp", false))
  {
    // true means image will act as popup
  }
 else
  {
     // false means image will act as link
  }
于 2013-05-27T21:43:48.160 回答