Customizing Glassview Editable Field

2 comments

Need to make a generic implementation using Glassview without passing expression as argument in Glass view Editable helper ?

I recently had to create a generic grid which can be used to display items of multiple templates.

For examples – If you have to create a Grid which can be used for Student and Staff templates. Each of these templates have different set of fields in it.

To make field editable – you should pass expression with field name as “x=> x.studentname” as argument. Now we have Staff template which should display “x=> x.staffname”.

To tackle these situations , I created a template to store the mapping fields in Sitecore and it can be used to display the Grid.

Lets see the code:

Code to extend Editable helper

 public abstract class CustomGlassView : GlassView where TModel : class
    {
        public HtmlString FieldEditable(T model, string fieldName, object attributes = null,
    bool isEditable = true, string contents = null, bool isContentEmpty = false)
        {

            var propName = SitecoreFieldName(fieldName);
            if (!string.IsNullOrEmpty(propName))
                fieldName = propName;
            ParameterExpression selectedProperty = Expression.Parameter(typeof(T), "x");
            Expression memberExpr = Expression.PropertyOrField(selectedProperty, fieldName);
            var lambda = Expression.Lambda(memberExpr, selectedProperty);
            return base.Editable(model, lambda, attributes);
        }

        public string SitecoreFieldName(string fieldName)
        {
            var propertyName = string.Empty;
            var properties = typeof(T).GetProperties();
            foreach (var property in properties)
            {
                var attribute = property.GetCustomAttributes(typeof(SitecoreFieldAttribute), true).FirstOrDefault() as SitecoreFieldAttribute;
                if (attribute != null ? attribute.FieldName == fieldName : false)
                {
                    propertyName = property.Name;
                    break;
                }
            }
            return propertyName;
        }
    }

 

2 comments on “Customizing Glassview Editable Field”

  1. Hello, There’s no doubt that your blog may be having browser compatibility problems. When I take a look at your site in Safari, it looks fine however, if opening in Internet Explorer, it has some overlapping issues. I just wanted to give you a quick heads up! Aside from that, excellent blog!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.