4

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.

3

2 Answers 2

10

And if you want to stick with the GET method, you can use the fact that browsers won't send disabled fields. So tie into the onsubmit handler and disable any empty hidden fields like the following:

@using (Html.BeginForm("Calculate", "Home", FormMethod.Get, new {onsubmit="DisableNullFields();"}))
{
    @Html.TextBoxFor(x => x.Test)
    <input type="text" name="TestField" />
    <input type="hidden" name="hidden" value="" />
    <input type="submit" value="Push"/>
}

<script>
    function DisableNullFields() {
        $('input[type=hidden]').each(function(i) {
            var $input = $(this);
            if ($input.val() == '')
               $input.attr('disabled', 'disabled');
        });
    }
</script>
5
  • This works great for hidden fields, however, what if my search string is empty? Is there a way to disable/hide it when submit is clicked, but before the form is submitted?
    – Rudey
    May 20, 2014 at 22:14
  • 1
    Do the same thing but widen your jQuery selector like $('input') or something. May 21, 2014 at 1:20
  • 6
    Ok that's twice you've done that now when the first answer was a solution to the problem as you originally described it. Very bad etiquette. I'm not going to do all of your work for you. I've pointed you in the right direction and it's your responsibility to figure out how to handle edge cases. Don't try to get me to come back and do more work for you by unchecking my answer as correct. You're on your own. May 21, 2014 at 22:16
  • Using Jquery's filter: $('input[type=hidden]').filter(function(){ return this.val()=='' }).prop('disabled', true); // attr may work as well.
    – Mark Ball
    Jun 18, 2019 at 19:34
  • Instead of adding the disabled attribute, you could've removed the name attribute, which would effectively prevent that input element from being submitted in the form.
    – Mladen B.
    Aug 9, 2019 at 15:04
-1

You could use POST instead of GET. So you could replace the link with a form containing hidden fields for the parameters that you don't want to appear in the query string:

@using (Html.BeginForm("EventOverview", "BaseEvent", new { id = eventInfo.Key.OID, type = eventInfo.Key.XPObjectType.TypeName.GetShortTypeName() }, FormMethod.Post, null))
{
    @Html.Hidden("activeTab", "#scheduleLink")
    @Html.Hidden("session", eventInfo.Key.EventSchedules[0].SessionId)
    @Html.Hidden("hall", eventInfo.Key.EventSchedules[0].HallId)
    @Html.Hidden("client", eventInfo.Key.EventSchedules[0].BasePlace.PremieraClientId)
    <button type="submit">Make</button>
}

from Darin Dimitrov answer in :

hide parameters passing to controller in address bar (URL rewrite or something else)

2
  • Thanks for the answer, but POST is not an option for me in this case.
    – Rudey
    May 21, 2014 at 22:07
  • Using a POST (non-idempotent) request for read-only GET (idempotent) operations is an anti-pattern. One example is that your users won't be able to use the Back button properly. The consumer of your application should be able to understand what your application does, by looking at which HTTP method you use for each operation. More info: restapitutorial.com/lessons/httpmethods.html
    – Mladen B.
    Aug 9, 2019 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.