Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Can't obtain values from controls through javascript in razor page

I have a razor page where evrything is displaying fine, but I need to be able to read the values inside a few controllers to pass them as parameters in an call to prefilter some data:

@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model MyApp.Pages.IndexModel
@{
    ViewData["Title"] = "MyApp";
}

<div class="col-12 border p-3 mt-3">
    <h3>Filters</h3>
    <div class="col-12">
        <select id="portMultiselect" name="lsPorts" asp-items="@Model.Ports" multiple></select>
        <input id="searchString" type="text" name="searchString">
    </div>
</div>
    @* A "REFRESHABLE" TABLE GOES HERE...*@

@section scripts{

    @* REFRESH SCRIPT *@
    <script type="text/javascript">
            $(function () {
                setInterval(loadTable, 60000);
                loadTable();
            });

        function loadTable() {
            var select1 = document.getElementById("portMultiselect");
            var selectedPorts = [];
            for (var i = 0; i < select1.options.length; i++) {
                if (select1.options[i].selected) selectedPorts.push(select1.options[i].value);
            }
            var searchString = document.getElementById('searchString').value;
            if (searchString != "" || selectedPorts.length != 0) {
                debugger;
            }
                fetch('/Index?handler=IndexPartial', {
                    data: {
                        searchString: searchString,
                        selectedPorts: selectedPorts
                    }
                })
                    .then((response) => {
                        return response.text();
                    })
                    .then((result) => {
                        document.getElementById('refreshable').innerHTML = result;
                    });
            }
        </script>
}

As you can see, I'm trying to capture the selected values in my multiselect and in a text input to pass as parameters in the refresh script.

I'm correctly arriving to the expected endpoint, but Im never getting any values (It's always empty and zero) I even added a debugger in the Javascript code that I'm never hitting.

Why can't I read those values?

EDIT: I have seen other ways of dealing with these, such as databindings, but as mentioned here, the only way to avoid page reload is javascript and AJAX, so I still need to get these values from the javascript.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

At the end, I found a solution to my predicament as follows:

I changed the way I render the selectPicker, following the examples shown in this page:

@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model MyApp.Pages.IndexModel
@{
    ViewData["Title"] = "MyApp";
}

<div class="col-12 border p-3 mt-3">
    <h3>Filtros</h3>
    <div class="row">
        <div class="col-6">
            <p>Puertos</p>
            @Html.DropDownListFor(x =>
                x.selectPort,
                (IEnumerable<SelectListItem>)ViewData["MyPorts"],
                new
                {
                    @id = "portMultiselect",
                    @class = "form-control selectpicker",
                    @Value = @Model.Ports,
                    data_live_search = "true",
                    multiple = "multiple"
                })
        </div>
        <div class="col-6">
            <p>Nombre de variable</p>
            <input id="searchString" type="text" name="searchString">
        </div>
    </div>
</div>

This comes with certain changes to the model you're working with, you can follow instructions to bind data as T.Trassoudaine commmented, here

for this to work you need to add the specified references in your _Layout (for the bootstrap-select), you can check all the things you need to add here

And finally, you will need to work with AJAX in javascript to pass the parameters back:

<script type="text/javascript">
    $(function () {
        setInterval(loadTable, 1000);
        loadTable();
    });

    function loadTable() {
        var select1 = document.getElementById("portMultiselect");
        var selectedPorts = [];
        for (var i = 0; i < select1.options.length; i++) {
            if (select1.options[i].selected) selectedPorts.push(select1.options[i].value);
        }
        var searchString = document.getElementById('searchString').value.toUpperCase();
        var searchPorts = "";
        if (selectedPorts.length != 0) {
            searchPorts = selectedPorts.join(",");
        }
        $.ajax({
            url: '/?handler=IndexPartial',
            data: {
                searchString: searchString,
                searchPorts: searchPorts
            },
            success: function (data) {
                document.getElementById('refreshable').innerHTML = data;
            }
        })
    }
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!