What is the best way to trigger a controller call in ASP.NET Core after a select change

I have a dropdown menu on my webpage, and I want to trigger a controller call every time the selection changes, passing the chosen value as a parameter.

<form asp-controller="CompetitorsMarketplace" asp-action="Index" method="get" class="form-filters">
        <div class="row">
            <div class="col-md-3">
                <div class="form-group">
                    <label class="control-label">Marketplace</label>
                    <select id="selectChangedValue" class="form-control" asp-for="MarketplaceName"
                     asp-items="Model.MarketplaceSelectList"></select>
                </div>
               </div>
                <div class="col-md-3 text-left ">

                <button class="btn btn-info btn-filter"><i class="fa fa-search"></i>&nbsp;Filter</button>
            </div>
        </div>
    </form>

Controller

 public IActionResult Index(CompetitorByMarketPlaceViewModel competitorByMarketPlaceViewModel, string textSearchMarketplace, int page = 1)
{

}

And here is what I attempted using JavaScript:

<script language="JavaScript">
$('#selectChangedValue').on('change', function () {
    var postData = document.getElementById("selectChangedValue").value;

    window.location = "/CompetitorsMarketplace/Index?textSearchMarketplace=" + postData;
});
</script>

Answer №1

Razor makes it easy to incorporate URL actions and utilize the jQuery function .Replace to populate your postdata.

By utilizing '@Url.Action("Index", "CompetitorsMarketplace", new { textSearchMarketplace = REPLACE })' with window.location, you can easily replace 'REPLACE' with postData.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to access a particular property of a child component object from the parent component

Is there a way to access a child component's "meta" property from the parent component without using the emit method? I am aware of the solution involving an emit method, but I'm curious if there is a simpler approach to achieving this. // Defau ...

submit the JSON formatted data to the server

I am attempting to extract data from a form and transmit it to a remote server: Below is the code snippet: <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js"></script> </head> ...

How can I automatically submit a form upon page load with AJAX and receive the result in HTML format?

Attempting to automatically submit a form when the page loads using ajax and then retrieve the HTML (consisting of multiple divs that will be echoed on the AJAX URL) back to my AJAX page. Firstly, the code successfully auto submits the form but fails to t ...

Showing JavaScript #document within Selenium Environment

Having trouble scraping a table generated in Javascript using Python 2.7 and Selenium. The table code is visible when inspecting the webpage, but not in the page source accessible through Python. Tried various methods like changing iframes and refreshing ...

How can I automatically increase a field when editing a row in JqGrid for PHP?

While using JqSuite for PHP, I am attempting to automatically increment a field value every time I submit a row edit. However, my code doesn't seem to be working as expected! Snippet from grid.php: $custom = <<<CUSTOM var rowId; var keys, ...

Error: The document object is not defined when attempting to access it within a

<script type="module" src="/scripts/navbar.js"></script> <script> async function fetchContent(e) { e && e.preventDefault(); const response = await fetch('https: ...

Dealing with Asynchronous JavaScript code in a while loop: Tips and techniques

While attempting to retrieve information from an API using $.getJSON(), I encountered a challenge. The maximum number of results per call is limited to 50, and the API provides a nextPageToken for accessing additional pages. In the code snippet below, my i ...

Nested JavaScript function expression

In this JavaScript example, the snippet provided is for educational purposes and does not include validation. After defining the variable 'isBetween' within the buildBoundDetector() function, it raises questions about how passing a number through ...

Create visual representations using a JSON structure that includes timestamps for various locations

I need to create a JavaScript graph based on a JSON dataset containing locations and timestamps. The data is structured like an array, and my goal is to visualize the time spent at each location with a comprehensive graph. ...

Using jQuery's .load() function to exclusively receive image bytecodes

I am utilizing jQuery's .load() function to receive the bytecode of loaded images. Could it be due to a lack of specified MIMEType? because I'm using an image URL without a file extension? necessary to wrap the entire operation somehow? Here& ...

"Enhance Your Design with Hovering CSS Backgrounds

Seeking assistance with changing the background image on span hover. If anyone can assist, I have included the complete code for the website below. Take a look at the full website code here: Pastebin CSS Pastebin JavaScript ...

Import in ASP.NET

Is it possible to use #include in ASP.NET aspx source code to move part of the HTML code to a different .aspx page and call it when needed on the Parent.aspx page? I came across this website , but Visual Studio 2012's intellisense did not recognize th ...

Issue with Click event not working on dynamically added button in Angular 8

My goal is to dynamically add and remove product images when a user clicks the add or delete button on the screen. However, I am encountering an issue where the function is not being called when dynamically injecting HTML and binding the click event. Below ...

Ensuring proper alignment within anchor links and buttons

button, a { height: 30px; display: inline-block; border: 1px solid black; vertical-align: middle; } button > div, a > div { width: 30px; height: 10px; background-color: red; } <button> <div class="buttonDiv"></div> ...

Clarification: Obtain the state prior to the current one

I've been diving into the React Documentation and came across a topic that I'm struggling to grasp fully: https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state function Counter() { const [count, setCount] = useState(0) ...

The autofocus feature does not function properly in an input tag that is not located within a form element

Is there a way to set the autofocus property in an input element that is not part of a form? I have tried adding the "autofocus" attribute to the input tag but it doesn't seem to be working. <div> //I have added the autofocus property her ...

Implementing dynamic option selection in Vue using JavaScript and v-model

Is there a way to manage the chosen value in a v-modeled select tag using vue.js? I created a jsFiddle demonstration, but unfortunately, it is not functioning correctly. This leads to my inquiry: vm.$set('selected', '1') // does not wo ...

Submitting form data using Vue and PHPMailer

I've been encountering an issue with submitting a contact form from a page. After submitting the form, nothing seems to happen. My tech stack includes Vue, Axios, and PHPMailer for handling this functionality. Upon inspecting the network tab, it appea ...

A JavaScript technique for combining multiple arrays of objects based on a shared key

I am encountering issues with merging two or more arrays as per my desired outcome. I have two arrays named arrCustomer and arrCustomerDetails. They both contain CustomerID as a key, however, I aim to merge all values from arrCustomerDetails while incorpo ...

Utilize React-router to display child components on the webpage

Recently, I've encountered some challenges with this code. I have a component that takes in a child as a prop, which is meant to serve as the foundation for all the pages I am hosting. Base.jsx : import React from 'react'; import PropTypes ...