Copied to clipboard

Flag this post as spam?

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


  • andrew shearer 512 posts 661 karma points
    Sep 16, 2011 @ 07:05
    andrew shearer
    0

    "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]

        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?

     

     

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 16, 2011 @ 12:32
    Sebastiaan Janssen
    0

     

    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;
        }
    }

     

  • andrew shearer 512 posts 661 karma points
    Sep 19, 2011 @ 05:53
    andrew shearer
    0

    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?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 19, 2011 @ 13:04
    Sebastiaan Janssen
    0

    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!

  • andrew shearer 512 posts 661 karma points
    Sep 21, 2011 @ 03:46
    andrew shearer
    0

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

     

Please Sign in or register to post replies

Write your reply to:

Draft