The index method on my controller looks like this:
public ActionResult Index(string search, string sort, int? groupId)
For the search functionality, I have the following form:
@using (Html.BeginForm())
{
<div>
@Html.Label("search", "Search")
@Html.TextBox("search", ViewBag.Search as string)
@Html.Hidden("sort", ViewBag.Sort as string)
@Html.Hidden("groupId", ViewBag.GroupId as int?)
<input type="submit" value="Search" />
</div>
}
Viewbag.Search
, ViewBag.Sort
and ViewBag.GroupId
contain the last used parameters. These could be null
or ""
, and when they are, this is the URL I see when I use the search form:
...?search=foo&sort=&groupId=
How can I hide these empty parameters from the URL, so it looks like ...?search=foo
?
EDIT: as Jason Nesbitt said, you can disable hidden field to exclude them from the form. However, I also want to hide empty parameters that come from other things than hidden
fields, such as regular input
fields, and also select
lists.
RouteConfig
? Or the full URL?