Copied to clipboard

Flag this post as spam?

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


  • Tom Fulton 2030 posts 4990 karma points c-trib
    Mar 15, 2012 @ 19:43
    Tom Fulton
    0

    Converting string data to DataTypeModel manually

    Hello Razor experts,

    I'm trying to figure out if I can turn a string / IPropertyResult into an object via a RazorDataTypeModel. 

    Usually this is done automatically when you do Model.propertyAlias, but in my case I'm trying to use a variable property alias, so I need to use Model.GetProperty(myAlias, true);.  When doing this I get the string value as opposed to the custom DataTypeModel - I don't think GetProperty checks for DataTypeModels itself.

    Ex:

    Model.selectCallouts -- returns a DynamicNodeList (thanks to MNTP's RazorDataTypeModel)
    Model.GetProperty("selectCallouts") - returns IPropertyResult or string with .ToString() with the XML

    So I'm wondering if there is a way I can do the binding/casting myself from the string data?  Or some other way to achieve this?

    The situation is using MNTP to select callouts, I need to reuse the macro with a different property so wanted to make the property alias variable.

    Thanks,
    Tom

  • Rodion Novoselov 694 posts 859 karma points
    Mar 15, 2012 @ 20:13
    Rodion Novoselov
    1

    Hi. It doesn't seem that GetProperty is DataTypeModel-aware, but I think that it's possible to workaround it with calling TryGetProperty directly (what the dynamic infrastructure actually does behind the scene).

    Something like this:

     class MyBinder : GetMemberBinder {
      public MyBinder(string name): base(name, true) {}
      public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion) { return null; }
    }

    object myPropValue;
    bool success = CurrentModel.TryGetProperty(new MyBinder(myPropertyName), out myPropValue);

    Some complication here is that the GetMemberBinder itself is an absatrct class so you have to implement it on your own as above (though only its Name property is used later, so you don't have to bother about actual implementation). Also you will need either to use the CurrentModel property or to cast manually the Model property to DynamicNode or just to DynamicObject.

     

  • Tom Fulton 2030 posts 4990 karma points c-trib
    Mar 15, 2012 @ 20:49
    Tom Fulton
    0

    Hi Rodion,

    Thanks so much for the info.  Your solution worked great - note I needed to change TryGetProperty to TryGetMember though, which I think is what you meant.  This is the final code:

    @{
        object myPropValue;
        bool success = Model.TryGetMember(new MyBinder("_selectCalloutsLeft"), out myPropValue); // added _ for recursive

    // should check for success and myPropValue != null
        foreach (dynamic node in (DynamicNodeList)myPropValue)
        {
            <p>@node.Name</p>
        }
    }
    @functions {
        class MyBinder : GetMemberBinder
        {
            public MyBinder(string name) : base(name, true) { }
            public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion) { return null; }
        }
    }

    Note I also found another way to do this, but I think I like your way better.  Plus recursion seems to be broken with .GetProperty and .GetPropertyValue at the moment.  For the record:

        var myData = Model.GetProperty("selectCallouts", true).Value.ToString(); // this recursion doesn't actually work in v4.7.1.1
        var dataTypeType = typeof(uComponents.Core.DataTypes.MultiNodeTreePicker.MultiNodeTreePickerModelBinder);
        IRazorDataTypeModel razorDataTypeModel = System.Activator.CreateInstance(dataTypeType, false) as IRazorDataTypeModel;
        object instance = null;
        razorDataTypeModel.Init(Model.Id, myData, out instance);

        foreach (dynamic node in (DynamicNodeList)instance)
        {
            <p>@node.Name</p>
        }

    Thanks again!
    Tom

  • Gareth Evans 138 posts 329 karma points c-trib
    Mar 15, 2012 @ 20:51
    Gareth Evans
    0

    Hi Guys

    Awesome post, I've actually got that feature down for implementing next time I do some work on DynamicNode

    Gareth

Please Sign in or register to post replies

Write your reply to:

Draft