Seems like an obvious question, but I can seem to find any references to What model is, Root, currentPage, what?
I need to see if the currentPage has a propery set to do some branching logic. I was assuming that Model represents currentPage, but I am not 100% sure. I have tried:
Model.property == value
Model.GetProperty("propertyAlias") == value
My guess is that because it is a Dynamic Node, I need to some how cast it or tell the engine what concrete type it is, but I am not sure.
The property I am trying to access is in a base document type that all my other document types inherit from so there will not be a case where that property won't exist.
Model is indeed equivalent to the $currentPage param in xslt.
using Model.propertyAlias should get you the value of the object, if you're using Model.GetProperty("propertyAlias").Value, you'll get a string.
Be aware tho, using Model.propertyAlias may be casted to a different type than you'd expect...
For example, if I'd had a property with a string value of "3-4", Model.propertyAlias may evaluate this as a date, so you'd need to use Model.GetProperty("propertyAlias").Value to get the actual value you're expecting (or maybe after casting it to a type you're expecting)
The error message you are getting is a bit strange to be honest. As Dirk confirmed, Model is the current node and is a dynamic of the type DynamicNode.
When you try to get a property on the node as the example you provided, Model.seoRemoveHeader, you should either get the value of that property or a DynamicNull object. You should never get an exception.
If you try to compare a property to some other value, like your example, Model.seoRemoveHeader != 1, there should be three possible outcomes.
The seoRemoveHeader value is of the type int, and the comparison succedes.
The seoRemoveHeader value is of another type than int, and you get an apropriate exception for that.
The seoRemoveHeader property does not exist, and you get an exception like: Operator '!=' cannot be applied to operands of type 'umbraco.MacroEngines.DynamicNull' and 'int'
What version of Umbraco are you running, and have you in any way altered the Model object in code not shown here?
I'm guessing you mean Umbraco 4.6.1 ? I believe that Umbraco 4.6.1 had a quite experimental implementation of Razor, A LOT has changed from then to 4.7, I would highly recommend you to upgrade your site, if that is in anyway possible. I'm afraid I can't assist you much on the 4.6.1 implementation.
Accessing currentPage/Property from razor
Hello All,
Seems like an obvious question, but I can seem to find any references to What model is, Root, currentPage, what?
I need to see if the currentPage has a propery set to do some branching logic. I was assuming that Model represents currentPage, but I am not 100% sure. I have tried:
Model.property == value
Model.GetProperty("propertyAlias") == value
My guess is that because it is a Dynamic Node, I need to some how cast it or tell the engine what concrete type it is, but I am not sure.
The property I am trying to access is in a base document type that all my other document types inherit from so there will not be a case where that property won't exist.
Any help would be appreciated.
Thanks for your time.
Model is indeed equivalent to the $currentPage param in xslt.
using Model.propertyAlias should get you the value of the object, if you're using Model.GetProperty("propertyAlias").Value, you'll get a string.
Be aware tho, using Model.propertyAlias may be casted to a different type than you'd expect...
For example, if I'd had a property with a string value of "3-4", Model.propertyAlias may evaluate this as a date, so you'd need to use Model.GetProperty("propertyAlias").Value to get the actual value you're expecting (or maybe after casting it to a type you're expecting)
Cheers,
/Dirk
Thanks Dirk for the response. I tried that but get the following error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'seoRemoveHeader'
The code I am using looks like this:
@if (Model.seoRemoveHeader != 1) {
//do something
}
The error message you are getting is a bit strange to be honest. As Dirk confirmed, Model is the current node and is a dynamic of the type DynamicNode.
When you try to get a property on the node as the example you provided, Model.seoRemoveHeader, you should either get the value of that property or a DynamicNull object. You should never get an exception.
If you try to compare a property to some other value, like your example, Model.seoRemoveHeader != 1, there should be three possible outcomes.
Operator '!=' cannot be applied to operands of type 'umbraco.MacroEngines.DynamicNull' and 'int'
I am using umbraco 3.6.1 which looks like it uses razor differently, is that the problem?
I'm guessing you mean Umbraco 4.6.1 ? I believe that Umbraco 4.6.1 had a quite experimental implementation of Razor, A LOT has changed from then to 4.7, I would highly recommend you to upgrade your site, if that is in anyway possible. I'm afraid I can't assist you much on the 4.6.1 implementation.
yeah sorry 4.6.1, ok thanks for your help anyway.
is working on a reply...