3

I'm using jQuery.Form plugin to submit an array to an ASP.NET MVC (4) application.

Let's say I have an array:

var items = [ 1, 2, 3 ];

Submitting that array using jQuery.Form plugin that array will be sent as:

items[]: 1
items[]: 2
items[]: 3

(when using form-url-encoded content type)

But ASP.NET MVC does not understand that, to make MVC understand that I need to send either:

items[0]: 1
items[1]: 2
items[2]: 3

(include index)

or

items: 1
items: 2
items: 3

(no square brackets)

I can't submit as JSON because along with array and other data I also submit files.

Question: is there a way to either configure jQuery.Form to send arrays in a different format, or to teach ASP.NET MVC to understand item[] format?

THX-1138
  • 21,316
  • 26
  • 96
  • 160

1 Answers1

0

If the multipart/form-data encoding type is a possibility, then it should be possible to submit this as a JSON in tandem with posted files, using Javascript FormData.

var formData = new FormData();

var json = "json" // Assuming you can wrap this up as a JSON,
                  // and you're using a <input type="file">.

formData.append("FileData", $("input:file")[0].files[0];
formData.append("JsonData", json); //etc.

 $.ajax({
    url: 'UnwrapData',
    data: formData,
    type: "POST", //etc.

Then you could send it all up to a Controller that looks something like this, where the JSON is parsed and the file data can be unpacked:

public ActionResult UnwrapData(HttpPostedFileBase FileData, string JsonData)
{
   // where the JSON data is unwrapped, etc.
}
alex
  • 6,818
  • 9
  • 52
  • 103