1

I am submitting a form to the controller, the form consists of properties which one of them is an array.

$("#parsedQuestions input").val(JSON.stringify(questionsArr));
var formData = JSON.stringify($("#trainingForm").serialize());

$.ajax({
    type: "POST",
    url: '@Url.Action("SaveTraining", "Training")',
    data:  formData,
    error: function(jqXHR, textStatus, errorMessage) {
        console.log(errorMessage);
    },
    success: function(data) {console.log(data)} 
});

The questionsArr is an array, but it is being returned as a plain string like this:

"[[\"1\",\"11\",\"111\",\"1111\",\"1111\",\"11111\"],[\"1\",\"22\",\"222\",\"2222\",\"22222\",\"2222222\"]]"

My Controller

public async Task<IActionResult> SaveTraining([FromForm] KII_TrainingModel formData)
{
}

It's a 2-dimensional array supposedly.

In my model, I tried to make this property ParsedQuestions as string[][] and List. But I only get one element, which is the formatted string array.

I have looked everywhere to find a solution but can't find one. Any answers or ideas will be appreciated.

If I don't stringify it, it looks like this:

enter image description here

If I do, then it returns this:

"[[\"1\",\"11\",\"111\",\"1111\",\"1111\",\"11111\"],[\"1\",\"22\",\"222\",\"2222\",\"22222\",\"2222222\"]]"

Any suggestions even as to how to convert this string to an actual array?

3
  • It's JSON, because you specifically used JSON.stringify to convert it to JSON. Thus, you need to use a JSON module to decode it back to its component parts. Commented Sep 10, 2022 at 1:36
  • Even without the JSON.stringify it's being submitted as a string Commented Sep 10, 2022 at 1:38
  • it's being submitted as a string Commented Sep 10, 2022 at 6:34

2 Answers 2

1

I believe that the way that sets the array into the text field and retrieves it from the form, the array will be in string value.

Instead, you have to append the array into formData via FormData.append() similar to below:

for (let i = 0; i < questionsArr.length; i++) {
    for (let j = 0; j < questionsArr[i].length; j++) {
        formData.append(`ParsedQuestions[${i}][${j}]`, questionsArr[i][j]);
    }
}

And this line is no longer needed:

$("#parsedQuestions input").val(JSON.stringify(questionsArr));
Sign up to request clarification or add additional context in comments.

Comments

-1

I wrote this as a sample: you can access the data indexing into both arrays.

    let ar = [["1", "11", "111", "1111", "1111", "11111"], ["1", "22", "222", "2222", "22222", "2222222"]]
let ans =    ar[0][2];
console.log(ans);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.