'Umbraco custom controller not using declared model type

Umbraco 9 - I've created a page type called SiteSearch and a controller to hijack requests to pages of that page type. This all works correctly.

The controller gets an IEnumerable from a very simple search service and sets it on a ViewModel, which is then passed to the view.

However, I simply cannot get the view to respect the model declaration. I am getting the error:

ModelBindingException: Cannot bind source type Maysteel_Web.ViewModels.SiteSearchViewModel to model type Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent.

It seems to be forcing my view to use an instance IPublishedContent (singular), even though I'm declaring the model as my custom object. I've tried changing the model declaration to string just to see what would happen:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Maysteel_Web.ViewModels.SiteSearchViewModel', but this ViewDataDictionary instance requires a model item of type 'System.String'.

There it recognized the model, but when I declare my custom object as a model, it goes back to trying to bind to IPublishedContent (singular). I've verified that the model I'm passing is not null and actually has results. I'm not sure what else to try. Can anyone help me undertand why this is happening?

SiteSearchController action:

public override IActionResult Index()
    {
        var searchPage = new SiteSearch(CurrentPage, _publishedValueFallback);
        var results = _searchService.QueryUmbraco("about");
        var viewModel = new ViewModels.SiteSearchViewModel();
        viewModel.Results = results;
        return View("~/Views/SiteSearch/index.cshtml", viewModel);
    }

View:

@model Maysteel_Web.ViewModels.SiteSearchViewModel
@{
    Layout = "/Views/Main.cshtml";
}
<h1>Site Search Page</h1>

ViewModel:

public class SiteSearchViewModel
    {
        public IEnumerable<IPublishedContent> Results { get; set; }
    }


Solution 1:[1]

I just figured it out. My layout view had the following line in it:

@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage

Once I removed that, I no longer get the error. It must have been forcing the ViewModel to be IPublishedContent.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tjans