I have Html.BeginForm , here is its code:

 @using (Html.BeginForm()) { @Html.TextBox("searchString", (string)ViewBag.searchString, new { @class = "form-control", name = "searchString", placeholder = "Search..." }) <input type="submit" value="Search" class="btn" /> @Html.ActionLink("Search", "Index", new { sortOrder = @ViewBag.sortOrder, searchString = ???, page = 1 }, new { @class = "btn", type = "submit" }) } 

By pressing <input type="submit" value="Search" class="btn" /> all parameters are passed correctly, but I need to change one parameter ( page ) to 1, so if I am on page 4 and click the button all parameters will be transferred without changes, then after the search I will be on empty 4 page, and the first will be the search result.

I tried to pass all the parameters using Html.ActionLink , but could not find how to transfer the contents of Html.TextBox as a parameter.

Questions:

  1. How to change the page parameter by clicking the button ?
  2. How to pass the contents of Html.TextBox as a parameter?
  • one
    What do you want to achieve? Some form, for some reason, combined with pagination and sorting ... I may not understand something, but completely calmly remove Html. ActionLink - insert the usual button <button> and put all your page and sortOrder in Html.HiddenFor . Read here about the usual pagination , there are no forms. If forms are needed, then either supplement your question, or use the usual button as I wrote HiddenFor +. - AK
  • @AK on the page there is pagination, sorting and searching. I make it so that when I switch to another page, or when changing the sorting, all data is transferred, namely строка поиска and вариант сортировки . I did this with ViewBag. The problem is that when I am, for example, on page 4 and click on <button> to search, the page is transferred to the controller with all the parameters, and the page does not need to be transferred when searching, or it must be set to 1 . So I can not figure out how to do it .. - Nikita
  • Well, remove it from the page form, since it is unnecessary for you and generally constant. And in the controller, set the value by default int page = 1 And remove Actionlink altogether, replace it with button, and in sortOrder let it form from TextBox PS They would give the controller code and the view code completely - I wouldn’t have to explain it to me on my fingers - to you. - AK

1 answer 1

That's how I managed to solve my problem:

 <input type="text" name="searchString" value="@ViewBag.SearchString" class="form-control" placeholder="Search..." /> <input type="hidden" value="1" name="page" /> <input type="submit" value="Search" class="btn" />