Search code examples
htmlasp.net-corerazor-pagesasp.net-core-tag-helpers

Form Action Tag Helper in Razor Pages doesn't redirect me


I'm trying to do a search box. When I press the search button nothing happens. I want to redirect to /Search&searchTerm=il. Also how to get what's typed in the input box and add it to the asp-route-searchTerm?

<div class="input-group col-4">
  <input class="form-control py-2" type="search" placeholder="Search books and authors">
  <span class="input-group-append">
    <button asp-page="/Search" asp-route-searchTerm="il" class="btn btn-outline-secondary" type="button">
      <i class="fa fa-search"></i>
    </button>
  </span>
</div>


Solution

  • the other example is right as far as get, but rest is wrong tech Razor Pages:

    Html

    <form sp-page="./Index" method="get">
                    <div class="form-actions no-color">
                        <p>
                            <input type="text" name="SearchString" value="@Model.CurrentFilter" placeholder="Add search term" class="form-control" />
                            <button type="submit" value="Search" class="btn btn-primary btn-round">
                                <i class="material-icons">search</i>
                                Search
                            </button> |
                            <a asp-page="./Index">Back to full List</a>
                        </p>
                    </div>
                </form>
    

    Code Behind:

    declare a holding property

    public string CurrentFilter { get; set; }
    

    Your OnGetAsync

    public async Task<IActionResult> OnGetAsync(string searchString)
        {
            if (!string.IsNullOrEmpty(searchString)) { CurrentFilter = searchString; }
    

    Notice the form tag is:

    notice that OnGet takes in a string ( query string param called searchString )

    Notice that Text field has a name param.

    The extra property CurrentFilter is so it can be remembered and model bound. so paging, order by etc can also be in that URL