Try our conversational search powered by Generative AI!

Accessing local block property directly on PageData.Property with string index?

Vote:
 

Im wringing an extension that takes in a list of expression and a T value of PageData. The idea is that you should be able to specify a list of properties and the code will take the first property of that list if its set and return its string value with stripped html and shortened text.

At the moment the method looks like this:

        public static string GetPuffText<TPageData, TPropertyValue>(this TPageData pageData, int maxTextLength, string defaultValue,
            params Expression<Func<TPageData, TPropertyValue>>[] expressions) where TPageData : PageData
        {
            foreach (var expression in expressions)
            {
                var propertyName = Utils.Instance.GetPropertyName(expression);
                if (!string.IsNullOrWhiteSpace(propertyName))
                {
                    var propertyValue = pageData[propertyName];
                    if (propertyValue != null)
                    {
                        return propertyValue.ToString().StripHtml(maxTextLength);
                    }
                }
            }
            return defaultValue;
        }

    

This code work well for properties on the page. I could use this extension like this: @Model.CurrentPage.GetPuffText(100, string.Empty, x => x.Intro, x => x.MainBody);

But if the property i want to reach is within a local block on the page and the call would look like this: @(Model.CurrentPage.GetPuffText(100, string.Empty, x=> x.Listing.ListingText, x => x.MainContent.MainBody))

Then i wouldn't find anything. I would instead have to add x => x.MainContent as a parameter and then within the extension open up the MainContent block and access it's properties which wouldn't make my extension very generic


Is there any way i can access the local blocks properties directly with a string or in some other way that still makes this method as generic as it is now.

 

Thanks in advanced

#80523
Jan 24, 2014 10:16
Vote:
 

Hm why can't I edit the first post? Anyway... i ment "writing" not "wringing", haha

#80525
Edited, Jan 24, 2014 10:19
Vote:
 

Bump

#80625
Jan 28, 2014 9:39
Vote:
 

Maybe something like this?

            foreach (var expression in expressions)
            {
                var compiled = expression.Compile();
                TPropertyValue value = compiled.Invoke(pageData);
                if (value != null && !string.IsNullOrEmpty(value.ToString()))
                    return value.ToString();
            }

    

Although, you could change the property to accept a list of strings or objects, so that you could pass in other properties as well, that is part of your view model and not necessarily in the pagedata object.

Something like this, perhaps?

        private static string FirstNotEmpty(params string[] sources)
        {
            foreach (var source in sources)
            {
                if (!string.IsNullOrWhiteSpace(source))
                {
                    return source;
                }
            }
            return string.Empty;
        }

    

 

#80626
Jan 28, 2014 9:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.