3

I have an object array like the following.

var array=[
{
    Name:"name",
    Address:"address",
    Files://array of files
},
{
    Name:"name",
    Address:"address",
    Files://array of files
}
];

I need to pass the array through ajax and retrieve it on mvc controller. I am not getting both data and file at the same time as array in controller.

I have used formData in case of single object.But in the case of array I don't know how.Anyone please help

my code.. model..

public class DataModel{
   public string Name{get;set;},
   public string Address{get;set;},
   public HttpPostedFileBase[] Files{get;set;}
}

Controller..

    [HttpPost]
    public JsonResult AddData(List<DataModel> data)
    {

       //code          
    }

ajax//

$.ajax({
                    type: "POST",
                    url: '/Mydata/AddData',
                    data: {data:array},
                    contentType: false,
                    processData: false,
                    dataType: "json",
                    success: function (json) {
                      //some function
                    }
});
Binu
  • 31
  • 5
  • 1
    use form data http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc – Mir Gulam Sarwar Jun 19 '16 at 04:14
  • in case of object its ok.But how to pass object array instead?? – Binu Jun 19 '16 at 04:19
  • You need to use `FormData` but you also need to generate the correct names - `[0].Name`, `[0].Address`, `[0].Files`, `[1].Name`, `[1].Address`, `[1].Files` etc –  Jun 19 '16 at 04:35
  • Send your files data as blobs. Converting form input to json string will not work because your form contains multiple files so try converting the each file as blob. – Naga Srinu Kapusetti Jun 19 '16 at 04:37
  • @Stephen Muecke How to make FormData of this array? – Binu Jun 19 '16 at 04:41
  • `FormData.Append('[0].Name', 'someValue');` etc. But where do these values come from. If you generate you view correctly the you can just use `var formdata = new FormData($('form').get(0));` as per [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc) –  Jun 19 '16 at 04:51
  • @StephenMuecke Thank you.You saved my time.It just worked.. – Binu Jun 19 '16 at 09:20

1 Answers1

0

You can pass array by stringifying the array and pass it through data attribute of ajax.

var _myArray=JSON.stringify(array),
        $.ajax({
                //Rest of the code
                data: _myArray,
            });
brk
  • 48,835
  • 10
  • 56
  • 78
  • JSON.stringify will change files in the array to {}.So it won't work – Binu Jun 19 '16 at 04:26
  • 1
    Who is it that keeps up-voting completely wrong answers? –  Jun 19 '16 at 09:23
  • @Stephen Muecke can you justify this is a wrong answer – brk Jun 19 '16 at 11:13
  • Yes.If you stringify data, you must also use `contentType: 'json'` otherwise the `DefaultModelBinder` will not bind it, but you cannot send files using `contentType: 'json'`. Suggest you try it your self. –  Jun 19 '16 at 11:18