Copied to clipboard

Flag this post as spam?

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


  • damion-daly 5 posts 71 karma points
    May 11, 2023 @ 09:10
    damion-daly
    0

    Pass model data to an Umbraco 10 page

    Can someone point out what I'm doing wrong here?

    In Umbraco 10 I have a simple form on a nav bar which calls a controller method and passes in a coupe of parameters to do a search. The search works fine, I then want to open a page to show the results.

    form (this seems fine, it calls the controller as expected):

    <form id="test" method="post" class="form-group" asp-controller="Search" asp-action="Search">
                <input id="SearchInput" name="SearchInput" type="text" aria-label="Search Input" />
                <input type="hidden" id="RootId" name="RootId" asp-for="@rootId" />
                <button class="btn btn-primary" type="submit">Submit Search</button>
    </form> 
    

    Controller method (again, fine - correct search results are returned):

    public ActionResult Search(string SearchInput, string RootId)
    {
            var searchResults = _searchService.SearchAllSources(SearchInput, Convert.ToInt32(RootId));
            return View("SearchResults", searchResults);  
    }
    

    And the basics of the SearchResults page:

    @using MySite.Core.Models;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<SearchData>;
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    
    @{
        var m = Model;
        Layout = "_Layout";
    }
    
    <h2>Search Results total: @Model.totalResults</h2>
    

    Should the SearchResults page inherit from it's stronly-typed published model - searchResults - rather than the model used for the searchData? If so, how would I pass the results of the search to it?

    If I put breakpoints in the SearchResults page I can see the correct results in the line var m = Model, it then fails as it cant bind model SearchData to IPublishedContent

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    May 22, 2023 @ 14:30
    Frans de Jong
    0

    You are passing searchResults to the SearchResults view. What is the type of searchResults? Is it SearchData?

    If not you should replace SearchData with the type of searchResults in UmbracoViewPage<>

Please Sign in or register to post replies

Write your reply to:

Draft