mercredi 20 juillet 2016

Duplicate of parameters in URL caused by checkbox

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
        });
    }




Aucun commentaire:

Enregistrer un commentaire