Using Asp.Net.Core 2.2, Visual Studio 2019
I'd like to get the form data and the files in the same controller action. I've can get the files but not the form data or I can get the form data but not the files. How can I get both?
The return value is some Json that I use to pop up a message and to update the page using Jquery/Javascript.
I thought I had done this in but I can't find any examples in my own code libraries. I've been searching on here and the web and there don't seem to be any examples, which leads me to think (a) its trivial and I'm doing something stupid or (b) its not possible
What am I missing?
My Form
<form asp-controller="Admin" asp-action="EditProduct" id="frmItem" method="post" name="frmItem" novalidate="novalidate" enctype="multipart/form-data">
...
My Ajax
$("#uploadFile,#saveUpload").on("click", function (e) {
e.preventDefault();
var myForm = $("#frmItem");
var sUrl = myForm.attr("action");
var input = document.getElementById("inputGroupFile02");
var files = input.files;
var formData = new FormData();
for (var i = 0; i != files.length; i++) {
formData.append("importFile", files[i]);
}
formData.append("item", myForm.serialize());
$.ajax({
type: "POST",
url: sUrl,
contentType: false,
processData: false,
data: formData,
success: function (result) {
showNotify(result.message);
hide_modal();
},
error: function (err) {
showNotify("There was an error importing. " + err.statusText + " (" + err.status + ")", "warning", "fa fa-exclamation-triangle");
hide_modal()
}
});
});
My Controller
[HttpPost]
//[ValidateAntiForgeryToken]
public async Task<JsonResult> EditItem(Items item, List<IFormFile> importFile, int id)
If I place a break point in the controller and inspect, I can see files (importFile) or I can see data (item) but not both, depending on what method I use