Gå till huvudinnehållet Gå till huvudmenyn

PropertyFor not working with string properties after upgrading to Optimizely CMS 12 (a Quick fix)

Av Wezz Balk Lästid: 1 minut

This is a quick fix for a error with string properties we ran into during a Optimizely CMS 12 upgrade.

During an upgrade of Optimizely CMS from version 11 to 12 we ran into a strange error.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.String', but this ViewDataDictionary instance requires a model item of type 'Postnord.Web.Models.ViewModels.Pages.ArticlePageViewModel'.

The solution we upgraded is a standard MVC project with no real special view management, just a few custom paths that were added onto the default MVC locations.  After a whole lot of troubleshooting and eventually contacting Optimizely Support (they’re great) we got this answer from them: I found another case with the same issue. This is the customer's reply on how they solved it:

It was the way we handled display templates. In CMS 11 we made sure not to interfere with dynamic templates by modifying the view location, so our view was not found. That does not work in CMS 12 but rather you do the opposite and make sure display templates are found so that MVC never looks for our custom view format…

This led me to conclude that the view for rendering a string wasn’t properly located.

So, a quick solution to the issue was to add a Display Template in /Views/Shared/DisplayTemplates called String.cshtml with the content

@model String
@if (string.IsNullOrEmpty(Model))
{
return;
}
@Html.Raw(Model)

That solved the issue temporarily so I could continue with the upgrade.

Vi vill gärna höra vad du tycker om inlägget

1
    Wezz Balk
    Wezz Balk

    Utvecklare | Tech Lead | RnD Lead

    Läs alla blogginlägg av Wezz Balk