Copied to clipboard

Flag this post as spam?

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


  • npack 18 posts 179 karma points
    Feb 15, 2024 @ 15:54
    npack
    0

    Find block list item by Key

    I have an HTML form in a blocklist item. When that form is submitted, I want the receiving generic MVC controller to look up some Model.Settings from that same block list item.

    If I include a hidden field in my form that contains the "Key" guid of the Model.Settings, then in my destination controller I thought I would be able to look up that same block list item's settings but when I try

    umbracoContextReference.UmbracoContext.Content.GetById(hiddenKey)
    

    I get [null].

    If, however, I pass the key of my parent document (Umbraco.AssignedContentItem.Key,) then I am able to at least retrieve the document, after which I am looping through the document properties trying to find my block list then my block list item but it is feeling weird and the code is ugly and contains some fragile bits.

    Any idea if I'm on the right track or why block list items don't seem to be found by GetById()?

  • npack 18 posts 179 karma points
    Feb 15, 2024 @ 16:31
    npack
    0

    I experience the same behavior using an UmbracoHelper BTW

  • npack 18 posts 179 karma points
    Feb 15, 2024 @ 17:14
    npack
    100

    This is what the final code looks like. It requires that I not only send the block list element's key as a hidden field, but also the key of the containing parent document as a second hidden field. Let me know if you can think of a simpler way to do this.

                var parentPage = umbracoHelper.Content(parentPageKey);
                foreach (var property in parentPage!.Properties)
                {
                    if (property.PropertyType.DataType.EditorAlias == "Umbraco.BlockList")
                    {
                        foreach (var item in property.Value<BlockListModel>(null!)!)
                        {
                            if (item.Content is FormElement && item.Settings is FormSettingsElement)
                            {
                                FormSettingsElement formSettingsElement = (FormSettingsElement)item.Settings;
                                if (formSettingsElement.Key == formSettingsKey) // If there are two forms in the blocklist, making sure this is the one that was submitted
                                {
                                    string toAddress = formSettingsElement.RecipientEmailAddress!;
                                    string formName = formSettingsElement.FormName!;
                                }
                            }
                        }
                    }
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft