Transmit the array to the controller

Is there a way to pass an array to the controller? I attempted the following:

window.location.href = "/SomeController/SomeMethod?fields=" + SomeArray;

and also tried this:

window.location.href = "/SomeController/SomeMethod?fields[][]=" + SomeArray;

When I retrieve it in the controller, it comes out as:

public ActionResult SomeMethod(int[][] fields) // here fields = null;
{
// Some code
}

Answer №1

To utilize the capabilities of jQuery, implement the ajax method.

Transform the JavaScript object SomeArray into Json format and transmit it back to the controller within the data attribute of the ajax method. I demonstrate the use of JSON.stringify which is compatible with modern browsers; however, you have the option to include the script json2 for compatibility with older browser versions.


              $.ajax({
                    url: '/SomeController/SomeMethod',
                    type: 'POST',
                    data: JSON.stringify(SomeArray),
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    success: function (result) {
                        alert("Operation was successful");
                    },
                    error: function (xhr) {
                        alert(xhr.statusText + " Internal server error")
                    }
                })

Answer №2

For those utilizing jQuery, consider utilizing jQuery.param

var  info ={
    items :[
        [
            'apple',
            'orange'
        ]
    ]
};

window.location.href = 'NewController/NewMethod?'
                       + decodeURIComponent( $.param(info) );

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

Can ngFor be utilized within select elements in Angular?

I'm facing an interesting challenge where I need to display multiple select tags with multiple options each, resulting in a data structure that consists of an array of arrays of objects. <div class="form-group row" *ngIf="myData"> <selec ...

Revamping the values attribute of a table embedded in a JSP page post an AJAX invocation

I am encountering an issue with displaying the content of a table. The table's data is retrieved via an AJAX request when clicking on a row in another table on the same page. Here is my code for the JSP page: <table id="previousList" class="table" ...

Synchronized loops in jQuery using the .each method

I'm having trouble getting the ajaxStop function in jquery to work. Any suggestions on how to make it fire? My goal is to iterate through each anchor tag and update some content within it. After that, I want to use the ajaxstop event to trigger a scr ...

Seeking out the index of each instance of a specific element within an array using Ramda.js techniques

I am currently attempting to locate the index of all occurrences of both Header and Footer within an array. var arr = [ 'data', 'data', 'data', 'data', 'Header', 'data', 'data', 'd ...

Transform ActionResult and PartialView IEnumerable outputs into JSON objects for return

When it comes to converting a basic ActionResult to JSON objects and displaying them in a PartialView, what is the best approach? I want to update my application so that instead of only showing comments from the database at the time of the page request, it ...

Incorporate personalized buttons into your Slick Carousel

Looking to add custom previous and next buttons to a slick carousel? I attempted using a background image on the .slick-prev and .slick-next CSS classes, as well as creating a new class following the documentation, but unfortunately, the arrows disappeared ...

Is there a way to restore the face's original orientation after it has been rotated

Just to note, I am aware that this question has been asked before. However, the previous answers did not address my specific situation and requirements. Currently, I am developing a Rubik's cube using three.js. To achieve lifelike rotations, I am rot ...

Is it Necessary to Wait for my Script Tag in React when Utilizing a CDN?

My current project involves the use of a CDN to load a script, which I am implementing through the useEffect hook directly in my component. Here is the simplified code snippet: React.useEffect(() => { const script = document.createElement('scri ...

After a push to the router, scrolling is disabled

While working on a Vuejs project, I encountered an issue when trying to change the page of my PWA using this.$router.push();. It seems to work fine everywhere else except when doing it from a modal within a component. The pushed page loads but scrolling is ...

Creating a seamless rotation effect on an SVG shape at its center across all browsers, even Internet Explorer

Is there a way to make an SVG figure rotate around its center? I have been trying to calculate the rotation center and scale it based on the viewBox. It seems to work perfectly fine in Chrome, Firefox, and Safari, but I just can't seem to get it to wo ...

What is the best way to traverse a JSON object in AngularJS using a for loop?

I am facing an issue with iterating through an array and checking a condition for each element. If the condition is true, I need to return one value, otherwise another value. However, the loop is not terminating when the condition is met. Can anyone assist ...

Execute AJAX function following the completion of table loading from PHP in Ajax

I'm currently working on a shopping cart feature that involves calculating certain figures based on table values. The process involves loading the table using AJAX and PHP, which is functioning properly. However, I'm facing an issue where I nee ...

Vue function displays 'undefined' message

Although many posts on this topic exist already, I am struggling to understand what is going wrong (even after searching extensively here and on Google). I have created my interceptor like this, but I keep receiving an error message stating "This is undef ...

Jade not binding correctly with Angular.ErrorMessage: Angular bindings are

Struggling with simple binding in Angular and Jade. I've tried moving JavaScript references to the end of the document based on advice from previous answers, but still no luck. Any ideas on what might be wrong? File: angular.jade extends layout blo ...

Having trouble with the jQuery .addClass function failing to add a class?

I'm struggling to implement a highlighting feature for specific elements on my website. I've been utilizing the jQuery("selector").addClass("class") function, but it's not functioning as expected. function toggleHighlight(selector, on) { ...

A single pledge fulfilled in two distinct ways

My code ended up with a promise that raised some questions. Is it acceptable to resolve one condition with the token string value (resolve(token)), while resolving another condition with a promise of type Promise<string>: resolve(resultPromise); con ...

The Problem of Unspecified Return Type in Vue 3 Functions Using Typescript

Here is the code snippet I am working with: <template> <div> <ul v-if="list.length !== 0"> {{ list }} </ul> </div> </template> < ...

The Action Creator is not being waited for

In my application, I am using a placeholder JSON API to fetch posts and their corresponding users. However, I encountered an issue where the user IDs were being duplicated and fetched multiple times. To resolve this, I implemented the following code snippe ...

Mysterious AngularJS

I am currently working on minimizing and obfuscating my Angular code, however I have run into a complication. I came across the "Note on minification" page at http://docs.angularjs.org/tutorial/step_05 which has been helpful, but I'm defining my contr ...

Is it possible to determine if the clipboard is accessible in Firefox?

In my upcoming JavaScript project, I am looking to determine the accessibility of the clipboard. Particularly in Firefox where specific permissions need to be granted for each site in order to use certain functions like execCommand with cut, copy or past ...