Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Sep 12, 2013 @ 14:07
    Ismail Mayat
    0

    Datatypegrid null test

    Guys, I am using the awesome datatype grid in ucomponents. I have some text fields and media picker fields. The media picker fields are not mandatory and therefore will not always have a value in my code i have the following:

    var answers=new List<Answer>();
    
            var lib = new umbraco.MacroEngines.Library.RazorLibraryCore(questionNode);
    
            var answerXml = lib.ToDynamicXml(questionNode.GetProperty<string>(DocumentType.Questions.Question.Answers));
    
            foreach (var answer in answerXml)
            {
                var answerPoco = new Answer
                {
                    QuestionId = questionNode.Id,
                    Text = answer.answer.InnerText,
                    Weight = int.Parse(answer.weight.InnerText),
                    PercentageAnswered = answer.percentageAnswered.InnerText
                };
    
               //todo cyan / pink
                //DIES HERE HOW DO I TEST IT EXISTS
                if (answer.answerCyan != null && !string.IsNullOrEmpty(answer.answerCyan.ToString()))
                {
                    answerPoco.CyanImg = answer.answerCyan.InnerText;
                }
    
                if (answer.answerPink != null && !string.IsNullOrEmpty(answer.answerPink.ToString()))
                {
                    answerPoco.PinkImg = answer.answerPink.InnerText;
                }
    
                answers.Add(answerPoco);
            }
    

    I need to test that the property exists before trying to reference it. So answercyan and answerping are media pickers in the datatype grid datatype and will not always have a value.

    Regards

    Ismail

  • Jamie Pollock 174 posts 853 karma points c-trib
    Sep 12, 2013 @ 16:30
    Jamie Pollock
    1

    Hey Ismail I may be wrong, but are you looking to compare your dynamic property versus a DynamicNull?

    if (answer.answerPink.GetType() == typeof(DynamicNull)) {
      //Do whatever you need to do if the property doesn't exist
    }
    

    Jamie

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Sep 12, 2013 @ 17:07
    Ismail Mayat
    0

    Jamie,

    Weird its working now. So even if no value selected i will always have a blank node in the xml for the property so i just have to do

    answer.answerPink.InnerText!=string.empty

    No idea why maybe xml cache refresh or something but that works. Your answer if useful will keep in mind for similar in future.

    Regards

    Ismail

  • Jamie Pollock 174 posts 853 karma points c-trib
    Sep 12, 2013 @ 17:12
    Jamie Pollock
    0

    No problem, mate!

Please Sign in or register to post replies

Write your reply to:

Draft