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

105
Views
POST Javascript array to MVC controller .NET 5.0

im trying to send some data from my javascript array to my mvc controller. When i debug through im ending in my method public void GetJSArray(List<SelectorModel> selectorModels)

but selectorModels is null.

Here is my model.

public class SelectorModel
    {
        public int ID { get; set; }
        public string Cords { get; set; }
        public string Name { get; set; }
    }

here is my C# code in controller.

 [HttpPost]
        public void GetJSArray(List<SelectorModel> selectorModels)
        {
            //Code here           
        }

and here is my JavaScript/Ajax

function SaveInfoSpots() {
    var savedeSpots = JSON.stringify({ selectorModels: infospots });
    $.ajax({
        type: "POST",
        url: '/Home/GetJSArray',
        contentType: "application/json; charset=utf-8",
        dataType: 'JSON',//json
        data: savedeSpots,
        traditional: true
    });
}

If i make a console.log(savedeSpots) i get this. {"selectorModels":[{"ID":1,"Name":"Infospot1","Cords":"5000.00, 2293.85, 2278.05"},{"ID":1,"Name":"Infospot2","Cords":"1.94, 584.50, 5000.00"},{"ID":1,"Name":"Infospot3","Cords":"5000.00, -2705.97, -277.02"},{"ID":1,"Name":"Infospot4","Cords":"5000.00, 504.93, -2845.93"}]}

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Two place need modify in your code:

First, about model binding, add [FromBody] attribute on action parameter:

[HttpPost]
public void GetJSArray([FromBody]List<SelectorModel> selectorModels)
{
    //Code here           
}

Second, your API need an array not an object, so when you call the API, you need post the parameter like this:

[
    {
        "ID": 1,
        "Name": "Infospot1",
        "Cords": "5000.00, 2293.85, 2278.05"
    },
    {
        "ID": 1,
        "Name": "Infospot2",
        "Cords": "1.94, 584.50, 5000.00"
    },
    {
        "ID": 1,
        "Name": "Infospot3",
        "Cords": "5000.00, -2705.97, -277.02"
    },
    {
        "ID": 1,
        "Name": "Infospot4",
        "Cords": "5000.00, 504.93, -2845.93"
    }
]

not the {"selectorModels":...} format.

If you just need APIs, create webapi project is better than mvc project.

over 4 years ago · Santiago Trujillo Report

0

Add [FromBody] to your controller list param such as

public void GetJSArray([FromBody] List<SelectorModel> selectorModels)

Try removing

traditional: true

and

 charset=utf-8

from the ajax call as well. Should see the populated list then in the controller.

over 4 years ago · Santiago Trujillo 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!