0

i have a table with multiple rows , Each row has input file and some other text inputs , i want to pass this data to the JsonResult from jquery function , but i face the problem so i always get request files = 0

here is my code

function saveDocumentsData(researcherId) {
    debugger;
    var document = new Array();
    documents = new Array();
   $("#docsTable > tbody > tr").each(function () {
        var row = $(this);
        var id = row.find("span.id").html();
        var docId = row.find("span.docId").html();
        var docType = $("#docTypes" + id + " option:selected").val();
        var docDate = ($("#date" + id).datepicker('getDate'));
        var dFileUpload = $("#up" + id).get(0);
        var dFiles = dFileUpload.files;
        document =
        {
            "UpdateDate": thisDate, "IsActive": true, "UserId": userId,"JobResearcherId": researcherId,
            'JobResearcherDocumentsId': docId, 'JobResearcherDocumentTypesId': docType
            , 'DocumentRegisterDate': docDate.toISOString(), 'DocFiles': dFiles[0]
        };

       documents.push(document);

       });

    $.ajax({
     url: "@Url.Action($"AddResearcherDocuments", $"JobResearcher")",
        type: "POST",
        contentType: 'application/json',
        processData: false,
        data: JSON.stringify({
          researcherDocuments: documents
    }),
    success: function (data) {

    }
});
}

all data passed truly but inputs files . Any advice

Mahesh Khond
  • 1,297
  • 1
  • 14
  • 31
  • Use `FormData` and set the correct ajax options in order to upload files using ajax - refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) for an example. –  Nov 28 '16 at 05:41
  • ok , but how can i pass documents array in json.stringfy() . I tried that but its not working formData.append("researcherDocuments", JSON.stringify(documents)); – Abdel Rhman Mohmed Nov 28 '16 at 06:00
  • You cant. Just generate you form controls correctly in the first place –  Nov 28 '16 at 06:02
  • sorry , but what do u mean by generating from controls correctly – Abdel Rhman Mohmed Nov 28 '16 at 06:05
  • Create you view using a `for` loop or `EditorTemplate` - [refer this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) and then its just `var formdata = new FormData($('form').get(0));` (as per first link) and everything will be serialized correctly –  Nov 28 '16 at 06:08
  • ok , i got it , but can't i use this function and pass array of strings and array of files witin formdata – Abdel Rhman Mohmed Nov 28 '16 at 07:52
  • No, you need to add each name/value pair to `FormData` - and they need to be indexed. but why would you even consider that - just generate your form correctly (which clearly you not - you have invalid html for a start) –  Nov 28 '16 at 08:42

0 Answers0