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.
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.
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> }
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
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:
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.
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:
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:
Thanks again!
Tom
Hi Guys
Awesome post, I've actually got that feature down for implementing next time I do some work on DynamicNode
Gareth
is working on a reply...