Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
Passing a string array from JS to C# controller

I have an array in JS ( var selectedEmails = []; ) After I collect the selected Emails from a grid, I call a GET to C# controller:

 $.ajax({
            type: 'GET',
            url: '@Url.Action("SendEmailBatchs", "account")',
            data: { emails: selectedEmails },
            contentType: 'application/json',
            success: (data) => {
                console.log("data", data);
            }
        })

When called, the parameter in C# gets no data. I've checked in JS, and the emails are in the array. Using breakpoints, I've confirmed the Controller's method is getting called:

Controller:

[HttpGet("sendemailbatchs")]
 public async Task<ActionResult> SendEmailBatchs(string[] emails)
        {
            ...
            foreach (ApplicationUser user in users)
            {
                await SendEmailConfirmation(user);
            }
            return Ok;
        }

Changing the data to data: { emails: JSON.stringify(selectedEmails) }, will pass an array with 1 element only, containing a stringified list of emails "[\"loes.vea@phanc.com\",\"MaAlonso.Mfizer.com\",\"cech@mll-int.cm\",\"jier.aris@si.com\"]"

What would be the correct parameter from JS so that I get a string ARRAY where each email is an element of the array?

emails = [0]: "loes.vea@phanc.com\",
[1]: \"MaAlonso.Mfizer.com\", ...
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There is a gap between what you are expecting in the controller and what you are sending through ajax call.

public async Task<ActionResult> SendEmailBatchs(string[] emails)

Let's understand the parameter of the above function, you are expecting an array of strings and will refer to it as emails in the function body. So, you will have to pass an array of strings in the request body.

PFB my simple controller and corresponding ajax call:

[HttpGet("api/test")]
public ActionResult<int> RecieveData(string[] emails)
{
     return emails.Count();
}

Call from UI like this:

var settings = {
  "url": "https://localhost:5000/api/test",
  "method": "GET",
  "headers": {
    "Content-Type": "application/json"
  },
  "data": JSON.stringify([
    "john@example.com",
    "jane@example.com"
  ]),
};

$.ajax(settings).done(function (response) {
  console.log(response);
});
about 4 years ago · Juan Pablo Isaza Report

0

so, after following Devesh's suggestions I ended up with

var settings = {
    "url": '@Url.Action("SendEmailBatchs", "account")',
    "method": "GET",
    "headers": {
        "Content-Type": "application/json"
    },
    "data": {
        emails: JSON.stringify(selectedEmails)},
    };

the issue persisted, because I needed to DESERIALIZE the result, in C#:

public async Task<ActionResult> SendEmailBatchs(string emails)
{
    var selectedEmails = JsonConvert.DeserializeObject<string[]>(emails);
    //DO STUFF
}

Thx

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!