Best way to check type of properties on Razor Model object
Is there any good way to determine the type of a property on the Model object with Razor at design time? I'm using Umbraco 4.7.1.1, if that matters. I guess I'm just a stodgy old strongly-typed .net developer, but I've tended to lean on Intellisense to help me remember the properties and methods on variables and so forth. I make heavy use of the Razor Dynamic Cheat Sheet but it doesn't list the types of the default DynamicNode properties. When I started working with Razor it seemed like they were all strings (whether that made sense or not for a given property) but I've learned, for example, that CreateDate and UpdateDate are real datetime objects now. Is there a more strongly-typed way of working with Razor in Umbraco?
DynamicNull In the beta, when a property or type wasn't found, we just returned null. This broke handling, particularly with True/False types which didn't have a value (null instead of 0)
The 4.7 RC will return a new type, DynamicNull which you can test for. This was added so that a .Where against a property which existed on some nodes in the set but not on others, wouldn't crash. .Where explicitly checks for this type and returns a default value (false) An example of this is @Model.Children.Where("umbracoNaviHide != true") and not all of the nodes in your children have that property
this will print the current node type umbraco.MacroEngines.DynamicNode, which derives from a dynamic object and the properties are bound on the fly
Now, let's say that I want to get the type of the bodyText, i.e:
@Model.BodyText.GetType()
This will print the type System.Web.HtmlString which is the type bound to that property. Let's see another example:
@Model.Textpage.GetType()
Because the document type of the child items of the current node are "Textpage", then I will get a dynamic list of nodes of type umbraco.MacroEngines.DynamicNodeList
Anyway, hope it makes sense :) If you want to know the type of a certain property, just call the GetType() method and find out yourself :)
Hi. Actually there's a simple rule. If a property declared in the DynamicNode as a regular property (Name, Level, Url, WriterName, CreateDate, UpdateDate, etc) then its type will be the same as declared. Otherwise (i.e. the property is dynamically-dispatched) it will be a string.
I don't think the property will always be a string on a DynamicNode object. For example you can use Razor DataType Models to return a strongly typed object. Also since 4.7.1.1 there is an IntegerDataTypeModel which will return some data types (based on their GUID) as an interger.
Whoops... I've looked into code narrowly just to find out that I was wrong. Really, "yes/no", datetimes and richtext are all converted to their natural type. My apologies.
Thanks, guys, lots of good food for thought. I gave you all some karma. I'll have to try to those Razor DataType Models, although I've realized I can see the default properties' types if I create a new DynamicNode variable. Basically, I just need to play around with this stuff more.
Best way to check type of properties on Razor Model object
Is there any good way to determine the type of a property on the Model object with Razor at design time? I'm using Umbraco 4.7.1.1, if that matters. I guess I'm just a stodgy old strongly-typed .net developer, but I've tended to lean on Intellisense to help me remember the properties and methods on variables and so forth. I make heavy use of the Razor Dynamic Cheat Sheet but it doesn't list the types of the default DynamicNode properties. When I started working with Razor it seemed like they were all strings (whether that made sense or not for a given property) but I've learned, for example, that CreateDate and UpdateDate are real datetime objects now. Is there a more strongly-typed way of working with Razor in Umbraco?
Sure, you can get the type by using the following syntax:
Checking the type is very useful as explained in the Umbraco Razor Feature Walkthrough - Part 5 in the case where'd you like to check if the property exists:
DynamicNull
In the beta, when a property or type wasn't found, we just returned null.
This broke handling, particularly with True/False types which didn't have a value (null instead of 0)
The 4.7 RC will return a new type, DynamicNull which you can test for.
This was added so that a .Where against a property which existed on some nodes in the set but not on others, wouldn't crash.
.Where explicitly checks for this type and returns a default value (false)
An example of this is @Model.Children.Where("umbracoNaviHide != true") and not all of the nodes in your children have that property
You can explicitly check for it like this
Let's see if I can explain myself...
if you do something like this in your razor view:
this will print the current node type umbraco.MacroEngines.DynamicNode, which derives from a dynamic object and the properties are bound on the fly
Now, let's say that I want to get the type of the bodyText, i.e:
This will print the type System.Web.HtmlString which is the type bound to that property. Let's see another example:
Because the document type of the child items of the current node are "Textpage", then I will get a dynamic list of nodes of type umbraco.MacroEngines.DynamicNodeList
Anyway, hope it makes sense :) If you want to know the type of a certain property, just call the GetType() method and find out yourself :)
Cheers
Jorge
I think he's looking to get the type on a specific property, not necessarily Model itself.
Hi. Actually there's a simple rule. If a property declared in the DynamicNode as a regular property (Name, Level, Url, WriterName, CreateDate, UpdateDate, etc) then its type will be the same as declared. Otherwise (i.e. the property is dynamically-dispatched) it will be a string.
I don't think the property will always be a string on a DynamicNode object. For example you can use Razor DataType Models to return a strongly typed object. Also since 4.7.1.1 there is an IntegerDataTypeModel which will return some data types (based on their GUID) as an interger.
Jeroen
Whoops... I've looked into code narrowly just to find out that I was wrong. Really, "yes/no", datetimes and richtext are all converted to their natural type. My apologies.
Thanks, guys, lots of good food for thought. I gave you all some karma. I'll have to try to those Razor DataType Models, although I've realized I can see the default properties' types if I create a new DynamicNode variable. Basically, I just need to play around with this stuff more.
is working on a reply...