SearchBar on every page, should redirect to a Sear...
# help-with-umbraco
p
Hey everyone, i'm currently facing a problem where my code just won't hit my rendercontroller. I have a searchbar partial in my navigation which looks like this:
Copy code
html
<form method="get" action="@Url.Action("SearchPageQuery", "SearchPage")">
    <div class="searchBar input-group mb-3">
        <input id="search" name="search" type="text" class="form-control border-0" placeholder="Suche..." aria-describedby="button-submit">
        <button class="btn btn-outline-secondary border-0" type="submit" id="button-submit"><i class="fas fa-search"></i></button>
    </div>
</form>
Now i want my searchbar to redirect to my searchPage when clicking on the bar oder hitting enter. I have a render controller for the searchpage. But my problem is that the rendercontroller function does not get hit and i dont know why...
Copy code
cs
    
    public IActionResult SearchPageQuery()
    {
        SearchPageViewModel viewModel = null;

        if (!String.IsNullOrWhiteSpace("search"))
        {
            viewModel = ExecuteSearch("search", 1);
        }

        return CurrentTemplate(viewModel ?? GetDefaultViewModel());
    }
Ive also tried it with a index function and the parameters....>
Copy code
cs
    [HttpGet]
    public IActionResult Index([FromQuery(Name = "query")] string query, [FromQuery(Name = "page")] int page)
    {
        SearchPageViewModel viewModel = null;

        if (!String.IsNullOrWhiteSpace(query))
        {
            viewModel = ExecuteSearch(query, page);
        }

        return CurrentTemplate(viewModel ?? GetDefaultViewModel());
    }
s
Looks like you're not inheriting from : RenderController there. - also ensure the name of that controller matches your search page doc type. If you're reusing / sharing your search logic then it might be best to move that to a service helper class and call from both a page controller and API controller to do what you need. https://docs.umbraco.com/umbraco-cms/implementation/default-routing/controller-selection
p
The thing is that the searchbar is not really a page. It is more a partial which is in the mastertemplate in the navigation. So it does not have a fixed page where it is on. How can i get a function to be called by the form and what do i need for it? UmbracoController, rendercontroller aso.
h
I would use a surface controller to post your form to.
s
Really depends on what you're trying to achieve. When you say hitting Enter POSTs - does the user get taken to a specific search page with results or just render the page they are on with the url params and the search performed inside that "partial"? If it's the latter it sounds like your site probably needs a View Component. You can put the search url param logic into the view component which works a bit like a mini controller and then render the results accordingly. The way I tend to do it is a partial that calls a view component - so that I can quickly cache the results via the cached partial call (and it respects url params). But get it working as a view component first I think. https://docs.umbraco.com/umbraco-cms/v/10.latest-lts/reference/templating/mvc/viewcomponents
5 Views