Search code examples
visual-studio-lightswitchlightswitch-2013

Why contentItem.value is null- Lightswitch


I want to hide a control based upon a property. So I handle the PostRender event like this Lightswitch Printscreen

But I'm getting contentItem.value = null

How can I access the original object, so I can check the property that will hide the control?

Thanks


Solution

  • Based on the information you've provided it appears that, at the point of rendering the control, its value hasn't updated yet.

    As this isn't an unusual situation, the normal approach to handle this is to use LightSwitch's dataBind function which would be used as follows in your example: -

    myapp.ConfirmarRecepcion.GUIAItem_pagadoEntrega_postRender = function (element, contentItem) {
        // Write code here.
        contentItem.dataBind("value", function (value) {
            if (value.Documento.id != 1) {
                $(GUIAItem_pagadoEntrega).addClass(oculto);
            }
        }
    }
    

    Also, if value.Documento refers to a related entity, you should use the following approach to ensure its value has been retrieved: -

    myapp.ConfirmarRecepcion.GUIAItem_pagadoEntrega_postRender = function (element, contentItem) {
        // Write code here.
        contentItem.dataBind("value", function (value) {
            if (value) {
                value.getDocumento().then(function (documento) {
                    if (documento && documento.id != 1) {
                        $(GUIAItem_pagadoEntrega).addClass(oculto);
                    }
                });
            }
        }
    }