JavaScript evaluates HiddenFor ONLY when stop to debug it (ASP MVC4)

163 Views Asked by At

I have a problem evaluating a hidden field in JavaScript (ASP MVC4) I am using a model in my View and have a hidden input for a property in the model

@Html.HiddenFor(mdl => mdl.FilterByUser, new { @id = "filterByUserId" })

I have a Helper with a SearchBox, and on enter key pressed I am making the search.

$("#search-box").keydown(function (event) {
    var keypressed = event.keyCode || event.which;
    if (keypressed == 13) {
        var searchValue = $("#search-box").val();
        var filterByUser = $("#filterByUserId").val();
        debugger;

        window.location = "?searchValue=" + searchValue + "&filterByUser=" + filterByUser;

    }
});

The problem is that var filterByUser has a value ONLY if I switch on the DeveloperTools and the browser stops in the "debugger". If the tools are closed, I get "The parameters dictionary contains a null entry for parameter 'filterByUser' of non-nullable type 'System.Boolean' for method "

The other value, searchValue has no problem in being evaluated.

What can I do to fix this? Thank you, Daniel

1

There are 1 best solutions below

0
On

I got it. The problem was that the helper was already in a form. If I put a breakpoint/alert in the js keydown handler, it was executed. If not, the controller action was called using form.submit on textarea enter pressed.

My solve: the js handler was removed, and properties in form were adjusted to match the controller action properties.