Kalyani Shevale

Kalyani Shevale

  • NA
  • 3.2k
  • 705.5k

how to pass list and Images through Ajax in MVC

Sep 19 2018 6:52 AM
I want List of data and multiple images are added in view.then this all data are passed to the Action through Ajax.how to solve this problem help me....
  1. <input type="file"  id="file1" name="file" multiple />  
  2. <button type="button" id="SubmitQuiz" class="btn btn-default" style="margin-top: 10px;"><span class="glyphicon glyphicon-ok"></span> Submit Survey </button>
 jquery and ajax code
  1. for (var i = 0; i < arr.length; i++) {  
  2.   
  3.                                                                                                                resultQuiz.push({  
  4.   
  5.                                                                                                                    Id: Id,  
  6.                                                                                                                    Message: 'Rating Star',  
  7.                                                                                                                    selectedAnswerOptionID: arr[i].starId,  
  8.                                                                                                                    value: arr[i].value,  
  9.                                                                                                                });  
  10.                                                                                                            }  
  11.                                                                                                             
  12.                                                                                                             
  13.                                                                                                            var fileUpload = $("#file1").get(0);    
  14.                                                                                                            var files = fileUpload.files;    
  15.                                                                                                            var fileData = new FormData();  
  16.                                                                                                                          var lenimg = [];  
  17.                                                                                                                          for (var i = 0; i < files.length; i++) {  
  18.                                                                                                                              lenimg.push({  
  19.                                                                                                                                  names:files[i].name,  
  20.                                                                                                                              })  
  21.                                                                                                                              //fileData.append(files[i].name, files[i]);  
  22.                                                                                                                          }  
  23.                                                                                                                          debugger;  
  24.                                                                                                                          for (var j = 0; j < lenimg.length; j++)  
  25.                                                                                                                          {  
  26.                                                                                                                              resultQuiz.push({  
  27.   
  28.                                                                                                                                  Id: Id,  
  29.                                                                                                                                  Message: 'Images',  
  30.                                                                                                                                  selectedAnswerOptionID: lenimg[j].names,  
  31.                                                                                                                                  value: "",  
  32.                                                                                                                              });  
  33.                                                                                                                          }  
  34.                                                                                                                                                                                                                   
  35.                                                                                                                          $.ajax({  
  36.   
  37.                                                                                                                              type: 'POST',  
  38.                                                                                                                              url: '@Url.Action("QuizTest", "Quizz")',  
  39.                                                                                                                              //data:  fileData ,  
  40.                                                                                                                              data: { resultQuiz},  
  41.                                                                                                                              contentType: 'application/json; charset=utf-8',  
  42.                                                                                                                              dataType: 'json',  
  43.   
  44.                                                                                                                //processData: false,  
  45.   
  46.                                                                                                                //cache: false,  
  47.                                                                                                                success: function (response) {  
  48.   
  49.                                                                                                                    if (response != null) {  
  50.                                                                                                                        alert("Survey Save uccessfully.");  
  51.                                                                                                                    }  
  52.                                                                                                                    else {  
  53.                                                                                                                        alert("Survey Save uccessfully.");  
  54.                                                                                                                    }  
  55.                                                                                                                },  
  56.                                                                                                                error: function (response) {  
  57.   
  58.                                                                                                                }  
  59.                                                                                                            });  
  60.   
  61.                                                                                                            console.log(resultQuiz);  
  62.   
  63.                                                                                                        });  
  64.                                                                                                    });  
the Action is
  1.  public ActionResult QuizTest(FormCollection form,List<ViewModel.Answer.Answer> resultQuiz)  
  2.         {  
  3. return view();  
  4. }  
 

Answers (2)