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?

4

1 回答 1

0

如果multipart/form-data编码类型是可能的,那么应该可以使用 Javascript 将其作为 JSON 与发布的文件一起提交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.

然后你可以把它全部发送到一个看起来像这样的控制器,在那里解析 JSON 并且可以解包文件数据:

public ActionResult UnwrapData(HttpPostedFileBase FileData, string JsonData)
{
   // where the JSON data is unwrapped, etc.
}
于 2015-02-02T22:16:01.263 回答