Copied to clipboard

Flag this post as spam?

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


  • Keith Petersen 67 posts 111 karma points
    Jan 30, 2012 @ 18:33
    Keith Petersen
    0

    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?

  • Douglas Ludlow 210 posts 366 karma points
    Jan 30, 2012 @ 19:13
    Douglas Ludlow
    3

    Sure, you can get the type by using the following syntax:

    string type = Model.propertyName.GetType().ToString();

    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

    @using umbraco.MacroEngines;
    @if(@Model.propertyNameThatDoesntExist.GetType() == typeof(DynamicNull))
    { ... }
  • J 150 posts 489 karma points
    Jan 30, 2012 @ 19:20
    J
    1

    Let's see if I can explain myself...

    if you do something like this in your razor view:

    @Model.GetType()

    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 :)

    Cheers

    Jorge

  • Douglas Ludlow 210 posts 366 karma points
    Jan 30, 2012 @ 19:23
    Douglas Ludlow
    0

    I think he's looking to get the type on a specific property, not necessarily Model itself.

  • Rodion Novoselov 694 posts 859 karma points
    Jan 31, 2012 @ 00:38
    Rodion Novoselov
    1

    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.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 31, 2012 @ 09:32
    Jeroen Breuer
    1

    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

  • Rodion Novoselov 694 posts 859 karma points
    Jan 31, 2012 @ 10:54
    Rodion Novoselov
    0

    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.

  • Keith Petersen 67 posts 111 karma points
    Feb 01, 2012 @ 14:36
    Keith Petersen
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft