0

We are currently converting a large application from WebForms to MVC. Our application has an idea of, what we call, dynamic resources in that we have items in Sitecore that specify resources (CSS/JS) and those can be attached to a page so they can be changed on the fly if need be. With this, we have both a sublayout (ascx) and a rendering (cshtml) that does the work to get these resources on the page.

In the dynamic resource (DR) template, we have 4 fields:

  1. External CSS - Multi-Line Field
  2. Internal CSS - Tree Ex
  3. External JS - Multi-Line Field
  4. Internal JS - Tree Ex

We want our code to represent this template like so:

string[] ExternalCss{get;set;}
IDynamicResourceItem[] InternalCss{get;set;}
string[] ExternalJs{get;set;}
IDynamicResourceItem[] InternalJs{get;set;}

As you are aware, the string[] doesn't work because the Glass Mapper code doesn't inherently convert a Multi-Line Field to a string array; it puts the entire field value into the array as a single object.

I went and created an AbstractSitecoreFieldMapper and registered it and it works great as the model for the MVC rendering. However, the ascx doesn't work. We pull the template information via the sublayout's Parameters, which I thought was the underlying issue. But even in my testing, if I pulled the DR item and used item.GlassCast instead of GetRenderingParameters, it still didn't work. I believe the pipeline for the MVC model mapping uses the AbstractSitecoreFieldMapper while the other methods do not.

How do I go about mapping this correctly via GetRenderingParameters?

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Matt M
  • 1,093
  • 2
  • 11
  • 26
  • The method of mapping properties is the same for MVC and WebForms. I would first check what data is in the item before you do the Glass cast. – Michael Edwards Apr 30 '15 at 09:28
  • The difference between MVC and WebForms is that the MVC model for a view is instantiated via a pipeline. In WebForms, you have to use item.GlassCast or GetRenderingParameters (depending on what you're doing). The pipeline seems to respect AbstractSitecoreFieldMappers where the other two methods do not. – Matt M Apr 30 '15 at 12:37

1 Answers1

0

Why not use a Name Value List? You can give the various CSS/JS a logical name for easier identification in the "name" part which is not used and run through the System.Collections.Specialized.NameValueCollection that's created by Glass for this type of field to render?

IvanL
  • 2,475
  • 1
  • 26
  • 39
  • Though that would probably work, it's not necessary and we will definitely need to do something like the above in the future and we'd like to know how to do it properly. – Matt M Apr 30 '15 at 18:16
  • I always prefer to use standard methods as solution instead of starting to use fields for something they're not meant for (ea mapping multiline field to string[]). If you really want to have string[] I suggest creating your own field type and mapping it to your preferred data thus not confusing developers of your solution in the future when their fields aren't mapped as expected. – IvanL May 04 '15 at 10:16