Umbraco

While working with Umbraco 9 I encountered an issue where the razor view rejected a newly created view model. The page displayed the following exception:

“ModelBindingException: Cannot bind source type DanMcilroy.Core.ViewModels.BlogViewModel to model type Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent”

Custom ViewModel Exception

The exception indicated that the razor View expected a Model type of IPublishedContent. To resolve this, amendments were required in both the Controller and ViewModel classes.

BlogViewModel

In the BlogViewModel, I inherited from the generated ModelBuilder class Blog, which implements IPublishedContent, and initialised the base constructor. This approach provides access to Blog data properties within the razor View while allowing further ViewModel extension.

BlogViewModel

BlogController

The BlogController required updates to instantiate the BlogViewModel and pass in the necessary parameters: CurrentPage and _publishedValueFallback.

BlogController

Blog Razor View

After implementing these changes, the error was successfully resolved, enabling the use of both Blog page data properties and custom ViewModel extensions.

BlogView