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

Make a request to the local API in a React Native mobile app by sending a GET request

Currently, I am working on a mobile application using React Native and utilizing SQL Server and NodeJS. The API for this project is located in my localhost. Upon running the application on an emulator, I encountered an issue when attempting to make a reque ...

submit a unidirectional post using jquery.post

I am working with a variable named test_string which is set to the value "hello." var test_string = "hello"; I want to send this variable to a PHP page in a one-way communication. I have attempted the following: $.post('php_page.php', test_str ...

Guide to verifying the property value following mocking a function: Dealing with Assertion Errors in Mocha

Based on a recommendation from a discussion on this link, I decided to mock the readFileSync function and then mocked my outer function. Now, my goal is to verify whether the variable's value has been set as expected. file.js const fs1 = require ...

What reasons underlie the existence of various methods for importing Modules in JavaScript?

I'm confused about the distinctions when it comes to importing a JavaScript module in various ways such as: CommonJS ES5 ES6 NodeJS Typescript What is the reason for having multiple methods of importing JavaScript modules? Is the concept of a "modu ...

Is storing HTML tags in a database considered beneficial or disadvantageous?

At times, I find myself needing to modify specific data or a portion of it that originates from the database. For instance: If there is a description (stored in the DB) like the following: HTML 4 has undergone adjustments, expansions, and enhancements b ...

By implementing setInterval, but I aim to deactivate it upon detecting user input

Currently, I have a function being called using setInterval that loads my page via an ajax get request and displays the response in real-time like a live updates page. The issue arises when users are inputting data into the form because the page is refresh ...

Having trouble getting the jQuery autocomplete feature to work on textboxes that are generated dynamically

I've encountered an issue with implementing autocomplete on dynamically generated textboxes. Despite searching online, I haven't been able to find a suitable solution yet. Below is what I have tried so far: JQUERY: function bindAutoComplete(cla ...

Tallying the number of messages on Facebook

Greetings everyone! I have been utilizing the Facebook JS SDK to retrieve the number of messages sent. Below is the code snippet: <script> function statusChangeCallback(response) { console.log('statusChangeCallback'); console.log ...

Display a modal dialog showcasing the progress while making an ajax request

I am working with an Excel sheet that contains over 30,000 records. My goal is to insert each record into a database and display a modal window with a progress bar showing the percentage of completion along with details such as total number of records, num ...

Retrieving the Bluemix container IP allocation status in JSON format

In an effort to streamline my container deployment process, I am utilizing the containers service provided by IBM Bluemix. My current goal is to automate IP address assignment and update DNS entries by creating a script for this purpose. The management of ...

Whenever I try to retrieve a value using the key from ModelBindingContext.ValueProvider.GetValue(key

When working with AngularJS to manipulate a complex parent object with different behaviors for its children server-side, I encountered an issue while implementing the CreateModel function as suggested in this answer. The problem arises when any call to bin ...

Tips for handling JSON data from a URL in a C# Web Service

Can anyone guide me on how to fetch and handle JSON data from a URL in a C# web service? I'm working on a C# web service project where I need to fetch JSON data from a specific URL. If possible, could someone provide an example code snippet that dem ...

Guide on efficiently navigating a website: utilize selenium python to effortlessly hover the mouse over a sub menu and click on desired options

Seeking to automate a web application called "Maximo" using Selenium in Python, I've successfully managed to log in with credentials and click on buttons. However, I'm stumped on how to hover the mouse over a submenu and perform a click action. ...

Is it possible for two-way binding to function in index.html within Angular 4?

Does two-way binding function in index.html? I have some links and metadata in index.html. How can we define head parameters in a component? <head> <meta name="og:title" content={{titleValue}}> <meta name="og:url" content={{urlValue}}> & ...

What causes Chrome to crash when dealing with lengthy tail files?

My objective is to display a log file in real-time using a websocket connection. However, I am facing performance issues with Chrome when the paragraph ('p') element in the HTML becomes large (about 450 lines). This is the current implementation ...

Submitting the form without utilizing Ajax, but instead sending data directly to a PHP script

I've encountered an issue while posting a form to a PHP file using AJAX. Despite my efforts, the form is bypassing AJAX and posting directly to the PHP file. Here is my form: <form id="editform" name="editform" action="ajaxeditform.php" method= ...

Unfamiliar function detected in the updated Vue Composition API

I am currently in the process of converting a basic SFC to utilize the new Vue CompositionAPI. The original code functions perfectly: export default { data() { return { miniState: true } }, methods: { setMiniState(state) { if ...

What is the process for installing fontawesome using npm?

I encountered an error while attempting to install that looks like the following: $ npm install --save @fortawesome/fontawesome-free npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\Admin\Desktop\package.json&a ...

There seems to be an issue with the rangeslider.js jQuery plugin not being recognized as a function within the project. However, there are

I am currently working on integrating a Range-slider into my Django project with the help of rangeslider.js. I was able to successfully create a functional example on Codepen at https://codepen.io/Slurpgoose/pen/GRRpmpX, and everything seemed to be running ...

Guide to sending a JSON object in typical web forms using .Net

I am trying to understand how to call a method that performs a task and then returns a JSON object. Although I am new to working with JSON, I have a default.aspx file with the following code. Now, I want a regular method in Default.aspx.cs to execute upon ...