0

We're having a problem with the following: If our checkbox is checked, our url has two of the same parameters in it:

http://localhost:63978/Failure?ShowResolvedFailures=true&ShowResolvedFailures=false

We are almost sure that is has to do with the hidden parameter from the checkbox (See: asp.net mvc: why is Html.CheckBox generating an additional hidden input)

The first "ShowResolvedFailures" would be the value which is generated because of the fact that the checkbox is checked. The second "ShowResolvedFailures" is from the hidden property we expect

But how do we remove that parameter from our url. Of course we do not want to see the hidden parameter in our URL.

Here is our code:

cshtml:

<form asp-controller="Failure" asp-action="Index" method="GET" role="form" id="searchForm" asp-antiforgery="false">

<div class="pull-right">
    <span>
        Toon opgeloste storingen?
        <input asp-for="@Model.ShowResolvedFailures"
               type="checkbox"
               class="customCheckbox"
               onclick="this.form.submit();">
    </span>
</div>

controller.cs:

        [HttpGet]
    public async Task<IActionResult> Index([FromQuery] FailureIndexViewModel requestModel)
    {
        var showResolvedFailures = requestModel?.ShowResolvedFailures ?? false;

        var searchQuery = new FailureSearchQuery
        {
            CorrelationId = requestModel?.CorrelationId,
            CommandId = requestModel?.CommandId,
            ShowResolvedFailures = showResolvedFailures
        };

        var urlWithQueryString = QueryHelpers.AddQueryString(@"api/tool/failures/search", searchQuery.GetQueryParameters());
        var failureOverviewModel = await GetDataAsync<FailureOverviewModel>(urlWithQueryString);

        return View("Index", new FailureIndexViewModel
        {
            CorrelationId = requestModel?.CorrelationId,
            CommandId = requestModel?.CommandId,
            ShowResolvedFailures = showResolvedFailures
        });
    }
Community
  • 1
  • 1
E. Verdi
  • 310
  • 2
  • 19

2 Answers2

1

My personal view (may be stupid one) is this is design issue. there are two fields created for check box, one is checkbox itself and other one is hidden input. Hidden input is always false for some reason (that I don't know) and value of check box depends on whether it is checked or not. If it is not checked query string will be false because check box will not send anything if it is unchecked instead value will be of hidden input. But when check box is checked then it will send true and hidden input will send false which is your case. I would use work around for that.

Replace

var showResolvedFailures = requestModel?.ShowResolvedFailures ?? false;

with

var showResolvedFailures = Request.QueryString["ShowResolvedFailures"].Contains("True") ? true : false;
Imad
  • 7,126
  • 12
  • 55
  • 112
  • Hi Imad. The reason for the hidden input to be false is clear to me. See the link i added into my question. I also understand why both are send to the server. But my question is: Why are both visibile in the URL. I think it is because of the fact that Index is a HttpGet. But we can't make a Post of this method because it's the Index... I will try the Request.QueryString["ShowResolvedFailures"].Contains("True") because i saw that solution elsewhere. But where should i use this line? – E. Verdi Jul 20 '16 at 12:23
0

Use your own input checkbox construction instead the asp-for taghelper, This way you won't use the default template for boolean

 <input name="@Html.NameFor(m => mShowResolvedFailures)" 
        type="checkbox" 
        class="customCheckbox"
        onclick="this.form.submit();">
animalito maquina
  • 2,264
  • 3
  • 20
  • 35