Transmit information to the controller using jQuery in a C# MVC environment

Here is my jQuery script code snippet. The script works perfectly and stores the data array/object in a variable called dataBLL.

var dataBLL = [];
$('#mytable tr').each(function (i) {

dataBLL.push({

id: $(this).find('td:eq(0)').text(),
ctype: $(this).find('td:eq(1)').text(),
cpath: $(this).find('td:eq(2)').text(),
ckey: $(this).find('td:eq(3)').text(),
ckey: $(this).find('td:eq(4) input:frist').val(),

});
  $.ajax ({
             url:"User/BllBtn",
             type:"POST",
             data:"dataBll="JSON.stringify(dataBLL);
             dataType: "json",
             success: function (e) {

                alert("sucess");
            }})

However, I am facing an issue with sending this object/Array to my controller in order to utilize its data and iterate through each row entry.

This is the signature of my controller:

[HttpPost]
public ActionResutl BllBtn(List<string> dataBll)
{

}

I would appreciate guidance on how to convert this object into a list so that I can easily loop through it.

Answer №1

Your browser console has likely already informed you of a syntax error and invalid JSON being produced.

Try using data: { "dataBll": dataBLL }

as a solution, or

data: JSON.stringify({ "dataBll": dataBLL })

if necessary. Additionally, include

contentType: 'application/json; charset=UTF-8'

in the ajax call for another possible fix.

The next issue is attempting to accept List<string> in your method when dataBll is actually a complex object with properties such as:

id, ctype, cpath, ckey

You cannot define ckey twice within the same object, and your JSON object may not align correctly with the C# method type. Consider creating a new class, like so:

public class myNewType
{
  public string id {get; set; }
  public string ctype {get; set; }
  public string cpath {get; set; }
  public string ckey {get; set; }
}

and then accept List<myNewType> as the parameter for the method:

public ActionResult BllBtn(List<myNewType> dataBll)

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

Slide in parts gradually by scrolling up and down, avoiding sudden appearance all at once

I have implemented a slider on my website using jQuery functions. For scrolling down, the following code snippet is used: jQuery("#downClick").click(function() { jQuery("html, body").animate({ scrollTop: jQuery(document).height() }, "slow"); ...

What is the best way to retrieve the second to last element in a list

When using Protractor, you have convenient methods like .first() and .last() on the ElementArrayFinder: var elements = element.all(by.css(".myclass")); elements.last(); elements.first(); But what about retrieving the element that comes right before the ...

What is the best way to ensure a cron job executing a Node.js script can access variables stored in an .env file?

Currently, I have a scheduled job using cron that runs a Node.js script. This script utilizes the dotenv package to access API keys stored in a .env file. Running the Node.js script from the command line retrieves the variables from the .env file successf ...

Troubleshooting: Issues with AngularJS $route.reload() functionality

I have an angular app and I'm attempting to refresh the page. I've tried using $route.reload() as recommended in multiple posts, but I can't seem to get it to work (Chrome is showing me an error). Here's my controller: var app = angula ...

Remove model associated with tab on close in the PrimeFaces TabView

I am currently utilizing the Primefaces (version 3.0.1) p:tabView component, which is capable of displaying a dynamic number of tabs supported by a list in a model. These tabs are closable, and I aim to remove the corresponding list element when a tab is c ...

Step-by-step guide on achieving a radiant glow effect using React Native

I am looking to add a glowing animation effect to both my button and image elements in React Native. Is there a specific animation technique or library that can help achieve this effect? While I have this CSS style for the glow effect, I am uncertain if ...

Using Node.js and Express to upload a file seamlessly without needing to refresh the page

I'm in the process of developing a chat application with Node.js and I want to incorporate a file upload feature. However, every time I upload a file, the browser redirects to another link or refreshes the page, which disrupts the flow of the chat. I ...

Increase the space below the footer on the Facebook page to allow for an

I recently created a webpage at and noticed that it is adding extra height below the footer on my Facebook page: "" I am seeking assistance on how to remove this additional 21px height below all content in the footer. I have tried various templates but n ...

Trouble with using jQuery's find function when dealing with HTML responses from an AJAX call

I'm attempting to retrieve an HTML webpage using AJAX and then locate a specific div element $.get(url, function (data) { console.log($(data).find("div#container").html()); }); During debugging, I observe $(data) in the console as >> ...

troubleshooting problems with feathers.JS using the npm start command

After developing two separate feathersJS applications, I encountered a situation where running npm start resulted in two unique types of errors for each app. How can I go about resolving this issue? View image here https://i.stack.imgur.com/RrsGW.pnghtt ...

Create and save data to a local file using Angular service

I am facing an issue with my Angular service: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { person } from '../interfaces/iperson ...

What could be the reason for the "category empty" message appearing at the first index after clicking on the add

When I click on the add products button, why is the category name empty at the first index? 1. The text starts from index 2 of the category column. 2. When I change the value from the dropdown, it first displays the previous value in the category column an ...

React Native is facing difficulty in fetching pagination data which is causing rendering errors

Currently, I am working on fetching pagination data from an API. The process involves retrieving data from https://myapi/?page=1, then from https://myapi/?page=2, and so on. However, despite following this logic, I encountered an error that has left me puz ...

Unable to identify the element ID for the jQuery append operation

After attempting to dynamically append a textarea to a div using jQuery, I encountered an issue. Despite the code appearing to work fine, there seems to be a problem when trying to retrieve the width of the textarea using its id, as it returns null. This s ...

Is there a way to prevent this JavaScript code from deleting the initial row of my table?

Looking at the code provided, it's evident that creating and deleting new rows is a straightforward process. However, there seems to be an issue where the default/origin/first row (A-T) gets deleted along with the rest of the rows. The main requiremen ...

Attempting to showcase JSON response within an HTML page using JavaScript

Can anyone help me troubleshoot my code for displaying JSON data on a web page? Here's what I have so far: <button type="submit" onclick="javascript:send()">call</button> <div id="div"></div> <script type="text/javascript ...

Leveraging AngularJS and ng-map to incorporate interactive dynamic heatmap displays

Greetings everyone! I am completely new to frontend development and have embarked on my first journey with AngularJS. I must admit, it's quite challenging and I'm still trying to wrap my head around how it all works. Currently, I'm working o ...

Stop the sudden jump when following a hashed link using jQuery

Below is the code snippet I am working with: $( document ).ready(function() { $( '.prevent-default' ).click(function( event ) { event.preventDefault(); }); }); To prevent the window from jumping when a hashed anchor ...

Adding a context menu to a Leaflet map

Could someone provide guidance on how to add a custom context menu to a Leaflet map in Vue 3? I am currently utilizing [email protected], @vue-leaflet/[email protected], and experimenting with [email protected]. Here is a snippet of my code: ...

What is the process of matching a server response with the appropriate pending AJAX query?

Imagine a scenario where my web app utilizes AJAX to send out query #1, and then quickly follows up with query #2 before receiving a response from the server. At this point, there are two active event handlers eagerly waiting for replies. Now, let's ...