简体   繁体   中英

Umbraco: x.GetPropertyValue(“myProp”) vs x.myProp

I use Umbraco v4, but think this should be a common problem.

I have a generic property "myNode" of "Content Picker", that should obtain a DynamicNode...

so doying myObj.myNode I obtain the node itself ... so can use myObj.myNode.Url

But doying the myObj.GetPropertyValue("myNode") I obtain the ... string ID value of the node... so can't anymore do myObj.GetPropertyValue("myNode").Url (string does not have Url property)

I can't use directly myObj.myNode, because the name is "dynamic" (the same function should use "your"+"Node" or "their"+"Node" upon conditions - the example is very aproximative, but hope the idea is clear)...

I even did myObj.GetPropertyValue<DynamicNode>("myNode") , but the result was the same: "8124" (the node id)

So, how to obtain the real property value, not just string representation of it?

Your content picker does not contain a node, it contains an id of a node.

myObj.GetPropertyValue("myNode") does exactly what is says, gets the value of a property called myNode on the instantiated DynamicNode object. It is not designed to return the node itself.

If you want to return the node whose ID your 'myNode' property contains then you have to use that value in a call to instantiate another DynamicNode

DynamicNode myNewNode = new DynamicNode(myObj.GetPropertyValue("myNode"))

or

Model.NodeById(myObj.GetPropertyValue("myNode"))

Use somethings like: mynode = Umbraco.Content(CurrentPage.myNode).Url (for Umbraco 6 and 7) For Umbraco 4 i use this Model.NodeById(Model.myNode).Url; in a script file. (I think it need at least Umbraco 4.7.x)

See also https://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors/Content-Picker

A not so elegant solution, but at least a solution that work:

var linkNode = image.GetPropertyValue("imgLinkNode" + model._lang.ToUpper());
if (linkNode is string)
{
    string id = linkNode;
    linkNode = model.NodeById(id);
}
var linkNodeUrl = linkNode.Url;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM