0

I am trying to display field name using Glass Mapper in view rendering in following format:

    FieldName - FieldValue
    FieldName - FieldValue

Ex:

  • Country - USA

  • City - New York

I can pull FieldValue from Model using GlassMapper but is there a possibility to fetch FieldName using GlassMapper or do I need to settle with Dictionary items to display in place of FieldNames

Thanks Shiva

1

It depends how you configure field names matching to class properties. If you do it using attribute, e.g.:

[SitecoreField(FieldName = "__Sortorder")]
public virtual int SortOrder { get; set; }

Then you can get field name by accessing to attribute:

var attribute = (SitecoreFieldAttribute) Attribute.GetCustomAttribute(Model, typeof (SitecoreFieldAttribute));
var fieldName = attribute.FieldName;

If you have auto mapping then filed name property will correspond to it's name.

4
  • Thanks for the response, This is my how my model looks like. I am still not clear how can I use the field name in view rendering. [SitecoreType(AutoMap = true)] public class LegalEntity : ContentBase {/// <summary> /// Continent /// </summary> public virtual string Continent { get; set; } } }
    – Shiva
    May 21 '18 at 7:51
  • In that case your field name is Continent :-) Why do you need some code to get it?
    – Anton
    May 21 '18 at 7:58
  • I don't want to hardcode the field name. I am able to get field value using @Model.Continent but I am trying to get field name as well in the format mentioned in question [I need to display field name here] - Model.Continet
    – Shiva
    May 21 '18 at 12:49
  • 1
    I am not sure what are your requirements but sounds like that maybe you want something like this (or a variant of this): jammykam.wordpress.com/2017/09/20/show-title-when-blank May 28 '18 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.