1

UPDATE: per this response this is behavior of HTML5? I tried setting the parameters in hidden inputs and that works. Doesn't make much sense to me, but what do I know.

I have my form for a "next page" button set up like this:

<form id="next" method="get"
      asp-controller="Search"
      asp-action="Cities"
      asp-route-sortOrder="@ViewData["CurrentSort"]"
      asp-route-currentFilter="@ViewData["CurrentFilter"]"
      asp-route-pageNumber="@(Model.PageIndex + 1)">
    <input form="next" type="submit" class="page-btn" disabled="@nextDisabled" value="Next" />
</form> 

When I inspect the page, the form has the correct action url (example):

/Search/Cities?currentFilter=Test&pageNumber=2

But the request actually being made is to

/Search/Cities?

But when it hits the controller, all of the parameters are null. Here is the top of my controller:

[Route("Search/Cities")]
public ActionResult Cities(string SearchString, string sortOrder, string currentFilter, int? pageNumber)

I'm not sure what I'm missing here.

butthash3030
  • 173
  • 1
  • 10

1 Answers1

2

you have 3 choices. This code was tested

first the most popular, you have to use post

<form id="next" method="post"
          asp-controller="home"
          asp-action="test"
          asp-route-sortOrder="CurrentSort"
          asp-route-currentFilter="CurrentFilter"
          asp-route-pageNumber="1">

         <label for="searchString">Search</label>
        <input type="text" id="searchString" name="searchString"><br><br>

        <input form="next" type="submit" class="page-btn" value="Next" />
    </form>

in this case searchingString will be sent in a request body, all another params in a query string

second

    <a href="/Home/Test?sortOrder=CurrentSort&currentFilter=CurrentFilter&pageNumber=2">
        <button class="page-btn">Next</button>
    </a>

the second option will generate get request if it is so important for you, but you will not be able post search string except using ajax.

third, you can use get, but route params should be in the inputs, probably hidden, search string will have a visible input

<form id="next" method="get"
          asp-controller="home"
          asp-action="test">
        
        <input type="hidden" id="sortOrder" name="sortOrder" value="CurrentSort" />
        <input type="hidden" id="currentFilter" name="currentFilter" value="CurrentFilter" />
        <input type="hidden" id="pageNumber" name="pageNumber" value="23" />


        <label for="searchString">Search</label>
        <input type="text" id="searchString" name="searchString"><br><br>

        <input form="next" type="submit" class="page-btn" value="Next" />
    </form>

in this case nothing will be inside of the body, everything in a query string including searchString.

Serge
  • 40,935
  • 4
  • 18
  • 45