0

I have a controller which is called on submit of a search field in my index view.

  [System.Web.Mvc.HttpGet]
    public ActionResult ShowResultsWithFilter(SearchModelWithFilters searchModel)
    {
        //Logic and setting viewbag values

        return View(searchModel);
    }

The url returns with my results. as

"url/Result/ShowResultsWithFilter?searchString=ASTRINGTOSEARCH"

My submit model

public class SearchModelWithFilters

    public string searchString { get; set; }
    public bool filterA { get; set; }
    public bool filterB { get;  set; }
    public bool filterC {get; set;}

    public SearchModelWithFilters()
    {
        //My initial values which should be defaut when first loading the results page
        filterA = true;
        filterB = true;
        filterC = true;
    }
}

The search and filter is also on the results page, so i then resubmit with new or the same details whilst using the same controller and the url returns as

"url/Result/ShowResultsWithFilter?searchString=ASTRINGTOSEARCH&filterA=false&filterA=true&filterB=false&filterB=true&filterC=false&filterC=true"

The first instance of each filter is always false and the second updates based on the values which are submitted.

I would like it to only show the correctly updated parameters(my filters), rather then double.

My search field view (SearchField)

@model CodeCompareUk.Models.SearchModelWithFilters

<div class="row">
    <div class="col-md-12">
            <div class="input-group input-group-lg">
                @Html.TextBoxFor(m => m.searchString, new { @class = "form-control", @placeholder = "Enter Search..." })
                <a href="javascript:$('form').submit();" class="input-group-addon">
                    <i class="fa fa-search"></i>
                </a>
            </div>
    </div>
</div>

I place it inside this form helper and in the results page also include the partial "refine search"

@using (Html.BeginForm("ShowResultsWithFilter", "Result", method: FormMethod.Get))
   {

       @Html.Partial("SearchField")

       //also this is only added in results page
       @Html.Partial("RefineSearch")


}

My refine search (RefineSearch)

@model CodeCompareUk.Models.SearchSubmitModelWithFilters
 @Html.CheckBoxFor(me => me.FilterA, new { @class = "custom-checkbox" })
  @Html.CheckBoxFor(me => me.FilterB, new { @class = "custom-checkbox" })
  @Html.CheckBoxFor(me => me.FilterC, new { @class = "custom-checkbox" })

Thanks

James Baxter
  • 129
  • 1
  • 8

1 Answers1

2

This is how CheckBoxFor works. It creates 2 <inputs> - one "hidden" and second "checkbox". Note that normally forms are submited with "POST" not "GET". Anyways, see more info about CheckBoxFor here:

Why does the CheckBoxFor render an additional input tag, and how can I get the value using the FormCollection?

Community
  • 1
  • 1
Nikolay
  • 10,752
  • 2
  • 23
  • 51