Вопрос

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 .

Это было полезно?

Решение 3

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
  }

Другие советы

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

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...
}

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
  }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top