What specific data am I sending from my ajax request to the MVC controller?

How can I determine the appropriate parameter to use when passing a JavaScript object?

Please see below:

var all = [];
//iterate through each instrument
for (var i = 0; i < 3; i++) {
    var txt = getThis(i);//int
    var detail =getThat(i);//string

    all.push({
        'this': txt,
        'that': detail
    });
}

ajaxCall.getNow("myUrl", {
    all
};

Here, ajaxCall.getNow is simply invoking the standard $ajax with get.

The main question is what kind of parameter should be used in my MVC controller.

If I use object, it works but not sure how to handle the object. It becomes quite useless as an object.

When attempting tuple<in, string>, it does not function.

For instance:

public JsonResult MyFunction(Tuple<int,string,string,double,double,string,int> all)//this fails


public JsonResult MyFunction(object all)//this works but I only have an ojbect, I@d like to have something like the tuple to work with

So, how do I determine the appropriate type of parameter for my MVC controller?

Answer №1

When making an ajax call, your code will transmit data in the following format:

{
    "all": [
        {
            "this": 0,
            "that": "some"
        },
        {
            "this": 1,
            "that": "some"
        },
        {
            "this": 2,
            "that": "some"
        }
    ]
}

To represent this data in C#, you can create the following classes.

public class All
{
    public int @this { get; set; }  //because 'this' is a reserved keyword
    public string that { get; set; }
}

public class RootObject
{
    public List<All> all { get; set; }
}

You can then use the RootObject as a parameter in your C# method.

public ActionResult SomeActionName(RootObject model)
{

}

Keep in mind that 'this' is a C# keyword, so consider using 'myThis' instead to avoid conflicts.

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

Having trouble signing out in Nextjs?

As a newcomer to Reactjs and Nextjs, I am currently working on developing an admin panel. To handle the login functionality, I have implemented the following code in my index.js/login page using session storage: const data = { name: email, password: pa ...

Having trouble retrieving input field values with Angular.js

I am struggling to access the input field values in my Angular.js application. Below is the code snippet I am using: <div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon ndrftextwidth text-right" style="width:180px"& ...

State change shows the previous and current values simultaneously

I've been working on updating the values of initUsers on the DOM. The initial state values work fine, but when I try to update initUsers, it displays different values than expected. Essentially, entriesNum receives a number as an event and changes th ...

Unable to grab hold of specific child element within parent DOM element

Important Note: Due to the complexity of the issue, the code has been abstracted for better readability Consider a parent component structure like this: <child-component></child-component> <button (click)="doSomeClick()"> Do Some Click ...

Choose the DIV element based on its data attribute using JSON

When using each(), my goal is to: Hide all divs where the data-infos.grpid = $jQuery(this).data('infos').grpid Show the next div where data-infos.ordre = $jQuery(this).data('infos').next_ordre I am unsure how to apply a "where" ...

Oops, the element type specified is not valid. Make sure to use a string for built-in components when working with NextJs

I am incorporating SVG's as a component in my nextjs application, but I encounter the following error message: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. ...

What steps do I need to take to successfully integrate Font Awesome 5 with React?

Here is an interesting scenario: the initial icon is displayed, but it fails to update when the class changes. const Circle = ({ filled, onClick }) => { const className = filled ? 'fas fa-circle' : 'far fa-circle'; return ( ...

Struggling with a persistent Jquery AJAX problem despite numerous attempts to resolve it

<table class="table table-striped"> <thead> <tr> <th>Choose</th> <th>>Location</th> <th>Topic</th> & ...

Performing cross-domain Ajax requests in Internet Explorer 10

I have created a web application that sends ajax requests to a web service on a server located in a different domain compared to where the app is hosted. The web service has been configured to perform a pre-flight check in order to set the necessary heade ...

Two tags attached to four hypertext links

Within my HTML code, I have hyperlinks present on every line. However, I am seeking to eliminate these hyperlinks specifically from "your previous balance" and "your new balance". In the following HTML snippet: <tr *ngFor="let l of statementLines; ...

What is the best way to adjust the size of an image within a div element using HTML?

I've created a Carousel with 5 images to display. However, I'm having an issue where only the image is showing up, and I want the text and button to appear below it. Here's how the nature image is currently displaying: What I want is for th ...

Audio playback system in Node.js

I'm looking to create a seamless playlist of mp3 files that play one after the other. While it may seem straightforward, I'm facing challenges keeping the decoder and speaker channel open to stream new mp3 data in once a song finishes playing. Be ...

Issue with displaying and hiding list elements using jQuery

I am attempting to create an accordion feature using <li> elements with classes .level1, .level2, .level3, and so on. The problem I am encountering is that when I click on a .level2 element, the items hide correctly until the next .level2 element wit ...

Resolve the route expression before the API request is fully processed

As a hobby coder, I have some gaps in my knowledge and despite trying various solutions, I have not been able to solve this issue. Idea Outcome My goal is to call my Express server, retrieve data from an external API, and render the data once it's f ...

assigned to a variable and accessed in a different route

Why does the "res.username" variable return as undefined in the second route even though my user needs to login before accessing any route? router.post('/login', passport.authenticate('local'), function(req, res) { res.username = r ...

How can I transfer form data to a PHP variable using an AJAX request?

Encountering some difficulties, any insights? I have included only the necessary parts of the code. Essentially, I have an HTML form where I aim to extract the value from a field before submission, trigger an ajax call, and fill in another field. It seems ...

How to retrieve the context of a .js file using jQuery $.ajax without automatically executing upon receipt

Upon fetching a *.js file using $.ajax, the scripts are executed upon receipt! Is there a way to fetch and execute it only when desired? Moreover, is there a method to remove these scripts at will? ...

Using JavaScript within HTML documents

Need help with inserting JavaScript code from Google: <script type='text/javascript'> googletag.cmd.push(function() { googletag.display('div-gpt-ad-1362706866260-0'); }); </script> into existing JavaScript / HTML code: va ...

Concentrating on div elements using React

Can the focus() method be used to focus on a div element or any other elements? I have added a tabIndex attribute to a div element: <div ref="dropdown" tabIndex="1"></div> When I click on it, I can see that it gets focused. However, I am att ...

Save the JSON data into a variable inside a React utility component

Currently, I am new to React and facing an issue with fetching data and storing it in a variable. I have been struggling to understand why my SetMovieResponse function is not working as expected. I have tried stringifying the JSON before sending it, but w ...