I have a page that displays a list of people. It can be sorted on first and last name. To search for people, I have the following Razor form:
@using (Html.BeginForm("Index", "Persons", new { sort = ViewBag.Sort }, FormMethod.Get))
{
<p>
Search: @Html.TextBox("search", ViewBag.Search as string)
<input type="submit" value="Search" />
</p>
}
ViewBag.Search
and ViewBag.Sort
contains the last used search
and sort
routeValues. When I sort the list of people on first name, the form gets rendered in HTML like this:
<form action="/persons?sort=firstname" method="get">
<p>
Search: <input id="search" name="search" type="text" value="" />
<input type="submit" value="Search" />
</p>
</form>
As intended, ?sort=firstname
is included in the action. However, when I press the submit button (Search), the sort
parameter is lost. The new url only has ?search=...
. How can I fix this?