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

132
Views
Array post through ajax is null in c# method

I am getting an array through a loop and passing it through an ajax call to my c# method but for some reason I am only getting null values in my parameter but count of the array comes through. Im not sure what I am doing wrong. For example the array before passing it through the ajax would have a count of 3, each key populated with the needed info, but as I pass it through the ajax call and debug on VS, the parameter for my list is null but has a count of 3?

let test = $('.divtest').map(function (index, element) {

    return {
        key1: $(element).closest('.testdiv')[0].id,
        key2: $(element).attr('id'),
        key3: $(element).attr('style')
    }
}).get();

Ajax Call

$.ajax({
    cache: false,
    type: "POST",
    data: { test: test},
    dataType: "json",
    url: "/controller/testhere",
    success: function (result) {
      alert('done');
    }
});

C# Method

 public JsonResult testhere(List<string> test)
    {
       //stuff
        return null;
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

your action is a list of string but ajax data is not. try this data

let test = [ "key1", "key2", "key3"];
.....
.ajax({
    cache: false,
    type: "POST",
    data: { test: test},
    dataType: "json",
    url: "/controller/testhere",
    success: function (result) {
      alert('done');
    }
});

or you will have to change the action

Create view model

public class TestViewModel
{
public string Key1 {get; set;}
public string Key2 {get; set;}
public string Key3 {get; set;}
}

action

public JsonResult testhere(TestViewModel test)
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!