I am using jQuery.filer on a FileUpload
control in an MVC5
project and I want to post multiple files from View
to Controller
=============================== S O L V E D ================================
Here is the solution by @StephenMuecke. Many thanks for his huge help...
View:
@model ExperimentViewModel
//Change 1
@Html.TextBoxFor(m => m.FileUpload, new { type = "file", multiple = "multiple" })
<script>
function create(event) {
event.preventDefault();
//Change 2 : Because jquery.filer adds "[]" to the Name parameter!!!
$('#FileUpload').attr('name', 'FileUpload');
var formdata = new FormData($('#frmCreate').get(0));
$.ajax({
type: "POST",
url: '@Url.Action("Create", "Experiment")',
cache: false,
dataType: "json",
data: formdata,
processData: false,
contentType: false
});
};
$('#FileUpload').filer({
//code omitted for brevity
//Change 3: Comment out uploadFile section
//uploadFile: { ... }
});
<script>
ViewModel:
public class ExperimentViewModel
{
//code omitted for brevity
//Change 4
[DataType(DataType.Upload)]
public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
}
Controller:
public JsonResult Insert(ExperimentViewModel model) //Change 5
{
if (ModelState.IsValid)
{
//...
}
}