4

I´m having issues passing an array of JSON objects to an MVC controller in ASP.Net core 3. The JSON object is built from a CSV file and passed through an AJAX call. The correct action is called but the object received is always null.

JS:

async function updateData(){
    var obj = await getData();
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/admin/metric/Update",
        data: obj,
        dataType: 'json',
        success: function (data) {
            console.log("SUCCESS : ", data);
        },
        error: function (e) {
            console.log("ERROR : ", e);
        }
    });
}

async function getData(){
    var metric = {};
    var metrics = [];

    var response = await fetch('https://bloburi.net/source/file.csv');
    var data = await response.text();
    var table = data.split('\n').slice(1);
    table.forEach(row => {
        var columns = row.split(',');
        metric["FirstName"] = columns[0];
        metric["LastName"] = columns[1];
        metric["Email"] = columns[2];
        metric["AverageHandleTime"] = columns[3];
        metric["AverageFollowUp"] = columns[4];
        metric["AverageHold"] = columns[5];
        metric["Date"] = columns[6];
        metrics.push(JSON.stringify(metric));
    });

    console.log(metrics);
    return metrics;
}

Models:

public class MetricBase
    {
        [Required]
        public string Date { get; set; }
        public double AverageHandleTime { get; set; }
        public double AverageHold { get; set; }
        public double AverageFollowUp { get; set; }
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

public class MetricModel
    {
        public IEnumerable<MetricBase> MetricBase { get; set; }
    }

Controller:

[HttpPost]
public IActionResult Update([FromBody]MetricModel obj)
{
    return Json(new { obj });
}

I think the issue may be on how I'm deffining the class that is receiving the JSON, but I've tried multiple things like deffining MetricBase as a List<> or straight up as an array in MetricModel, but it doesn't seem to work.

Any help is appreciated!

1 Answer 1

7

You adding the stringify item inside array via code

metrics.push(JSON.stringify(metric));

instead of stringify the whole array. Change the line to

metrics.push(metric);

and data: obj, to

data: JSON.stringify(obj),.

With the mentioned change, the $.ajax sends the whole array. Also change the action to

public IActionResult Update([FromBody]IEnumerable<MetricBase> obj)

Sign up to request clarification or add additional context in comments.

1 Comment

this works, the other thing I had to do is change the properties on MetricBase to strings only, I guess I'll just have to cast them into the corresponding data type. The only problem that came with this solution is that for some reason metrics.push(metric); is adding a copy of the last row of var table for every row that I have in var table, but at list is a different problem now, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.