Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben 1 post 71 karma points
    Sep 29, 2022 @ 09:12
    Ben
    0

    I am attempting to render a view to a search but I'm getting this error. I followed the documentation here : https://our.umbraco.com/documentation/reference/searching/examine/quick-start/

    I don't understand why this is happening and would be grateful if someone could provide a more detailed explanation. I'm a noob please don't crucify me. :)

    Error Message

    ModelBindingException: Cannot bind source content type Umbraco.Cms.Web.Common.PublishedModels.Search to model type MyProject.Models.SearchViewModel. The source is a ModelsBuilder type, but the view model is not. The application is in an unstable state and should be restarted.

    Search.cshtml

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @using Lucene.Net.Search.Similarities
    
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<MyProject.Models.SearchViewModel>
    
    
    <body>
    <div class="container content">
        <form action="@Model.Url()" method="get">
            <input type="text" placeholder="Search" name="query"/>
            <button>Search</button>
        </form>
        <div>
            @if (Model.SearchResults.Any())
            {
                <ul>
                    @foreach (var results in Model.SearchResults)
                    {
                        <li>
                            <a href="@results.Url()">@results.Name</a>
                        </li>
                    }
                </ul>
            }
            else if(Model.HasSearched)
            {
                <p>No results found</p>
            }
        </div>
    

    SearchViewModel.cs

     using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Cms.Core.Models.PublishedContent;
    
    namespace MyProject.Models;
    
    public class SearchViewModel : PublishedContentWrapped
    {
        public SearchViewModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback)
            : base(content, publishedValueFallback)
        {
        }
    
        public IEnumerable<IPublishedContent> SearchResults { get; set; } = Enumerable.Empty<IPublishedContent>();
        public bool HasSearched { get; set; }
    }
    

    SearchController.cs

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.ViewEngines;
    using Microsoft.Extensions.Logging;
    using MyProject.Models;
    using MyProject.Services;
    using Umbraco.Cms.Core.Models.PublishedContent;
    using Umbraco.Cms.Core.Web;
    using Umbraco.Cms.Web.Common.Controllers;
    
    namespace MyProject.Controllers;
    
    public class SearchController : RenderController
    {
        private readonly IPublishedValueFallback _publishedValueFallback;
        private readonly ISearchService _searchService;
    
        public SearchController(
            ILogger<RenderController> logger,
            ICompositeViewEngine compositeViewEngine,
            IUmbracoContextAccessor umbracoContextAccessor,
            IPublishedValueFallback publishedValueFallback,
            ISearchService searchService)
            : base(
                logger,
                compositeViewEngine,
                umbracoContextAccessor)
        {
            _publishedValueFallback = publishedValueFallback;
            _searchService = searchService;
        }
    
        public IActionResult ViewBook()
        {
            string queryString = HttpContext.Request.Query["query"];
    
            SearchViewModel viewModel = new(CurrentPage!, _publishedValueFallback)
            {
                SearchResults = _searchService.SearchContentNames(queryString),
                HasSearched = !string.IsNullOrEmpty(queryString),
            };
    
            return CurrentTemplate(viewModel);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft