i have some code that ensures the items specified in a [ucomponents multinode picker] property are actually published.
This works fine within my script file, but im trying to move this logic into a public @function within the App_Code folder.
my current code is: [where "referenceDocuments" is the multinode picker property]
var docs = @Model.referenceDocuments; List<dynamic> publishedMedia = new List<dynamic>(); foreach (var d in docs) { var mediaItem = Model.MediaById(d.InnerText); if (mediaItem.Id != 0) { publishedMedia.Add(mediaItem); } }
when i move this to a @function i get: "error CS1977: Cannot use a lambda expression as an argument to a
dynamically dispatched operation without first casting it to a delegate
or expression tree type"
my function is:
@functions{
@* function to obtain all of the currently published MEDIA for a given multinode picker property *@ public static dynamic PublishedMediaList(dynamic multinodePickerContent) { dynamic publishedMedia = new List<dynamic>(); foreach (var item in multinodePickerContent) { var mediaItem = @Model.MediaById(item.InnerText); if (mediaItem.Id != 0) { publishedMedia.Add(mediaItem); } }
return publishedMedia; } }
should 'dynamic' work in this context, or should by be trying to use something strongly typed?
So what's the problem with returning a List<dynamic> instead of a dynamic? if your method still is a public static dynamic but you return List<dynamic> you even still get a dynamic returned.
That said, if I change it to this, it works for me (you have to pass in the whole Model, not just the picker:
@functions{
public static dynamic PublishedMediaList(dynamic model)
{
dynamic publishedMedia = new List<dynamic>();
foreach (var item in model.ultinodePickerContent)
{
var mediaItem = model.MediaById(item.InnerText);
if (mediaItem.Id != 0)
{
publishedMedia.Add(mediaItem);
}
}
return publishedMedia;
}
}
thanks for the suggestion. in your example the "model.ultinodePickerContent" field is hard-coded into the function. To make it generic would you have to pass both @model and @model.ultinodePickerContent into the function?
No I wouldn't do that, if you want to make it independent from the datatype alias, I would pass in the alias and do model.GetProperty(alias) instead of model.ultimatePickerContent.
The uComponents guys will be working on a RazorDataTypeModel (see Gareth's blog post here), so this should become even easier to do soon!
i don't think model.GetProperty(alias) would work as that will try and return iProperty wouldn't it? it still needs to return the xml elements from the multinode picker property (a List of DynamicXml?)
"dynamic" and razor @functions
i have some code that ensures the items specified in a [ucomponents multinode picker] property are actually published.
This works fine within my script file, but im trying to move this logic into a public @function within the App_Code folder.
my current code is: [where "referenceDocuments" is the multinode picker property]
when i move this to a @function i get: "error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"
my function is:
should 'dynamic' work in this context, or should by be trying to use something strongly typed?
So what's the problem with returning a List<dynamic> instead of a dynamic? if your method still is a public static dynamic but you return List<dynamic> you even still get a dynamic returned.
That said, if I change it to this, it works for me (you have to pass in the whole Model, not just the picker:
thanks for the suggestion. in your example the "model.ultinodePickerContent" field is hard-coded into the function. To make it generic would you have to pass both @model and @model.ultinodePickerContent into the function?
No I wouldn't do that, if you want to make it independent from the datatype alias, I would pass in the alias and do model.GetProperty(alias) instead of model.ultimatePickerContent.
The uComponents guys will be working on a RazorDataTypeModel (see Gareth's blog post here), so this should become even easier to do soon!
i don't think model.GetProperty(alias) would work as that will try and return iProperty wouldn't it? it still needs to return the xml elements from the multinode picker property (a List of DynamicXml?)
is working on a reply...